Click here to Skip to main content
15,900,511 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalpthread_create Pin
Tuscon13-May-05 1:31
Tuscon13-May-05 1:31 
GeneralRe: pthread_create Pin
Priyank Bolia13-May-05 1:44
Priyank Bolia13-May-05 1:44 
GeneralRe: pthread_create Pin
Tuscon13-May-05 1:51
Tuscon13-May-05 1:51 
GeneralRe: pthread_create Pin
Priyank Bolia13-May-05 2:24
Priyank Bolia13-May-05 2:24 
GeneralRe: pthread_create Pin
David Crow13-May-05 2:11
David Crow13-May-05 2:11 
GeneralRe: pthread_create Pin
Bob Stanneveld13-May-05 4:00
Bob Stanneveld13-May-05 4:00 
GeneralDirectX - DrawIndexedPrimitive Pin
Marekh13-May-05 0:57
Marekh13-May-05 0:57 
GeneralDLL memory issue Pin
Andrew Hoole13-May-05 0:48
Andrew Hoole13-May-05 0:48 
Hi

I have been trying to write a dialog dll and have been having some real issues with it see (DLL and CTypedPrtList problem[^]). It seems to have moved away from my initial concerns of using CTypedPtrList in the DLL to a more general issue hence the new post.

I stripped the code down to the bare minimum removing all the interesting stuff and it still fails if I OK out of the dialog but not if I Cancel. To recap on the application.

void CTestApp::OnFileOpen() <br />
{<br />
	CLantisPatientSelectDlg dlg;<br />
<br />
	if(dlg.DoModal() == IDOK){<br />
		m_nPatID1 = dlg.GetPatID1();<br />
	}<br />
}


CLantisPatientSelectDlg is implemented in the DLL with exported constructors, DoModal() and GetPatID1() member functions.

void CLantisPatientSelectDlg::OnOK() <br />
{<br />
	int nIndex;<br />
	CPatientDetails *pPatientDetails;<br />
	CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_PATIENTLIST);<br />
<br />
	nIndex = pList->GetSelectionMark();<br />
	if(nIndex != -1){<br />
                // Do something interesting<br />
	} else {<br />
		m_nSelectedPatID1 = -1;<br />
	}<br />
<br />
	// Get any background list loading to terminate<br />
	m_bTerminateLoad = true;<br />
	::WaitForSingleObject(m_hListLoadingThread,INFINITE);<br />
	<br />
	CDialog::OnOK();<br />
}<br />
<br />
void CLantisPatientSelectDlg::OnCancel() <br />
{<br />
	// Get any background list loading to terminate<br />
	m_bTerminateLoad = true;<br />
	::WaitForSingleObject(m_hListLoadingThread,INFINITE);<br />
	<br />
	CDialog::OnCancel();<br />
}


Now I have done some debugging on this and it appears that when the program goes through the OnOK() function the line
m_nSelectedPatID1 = -1;
corrupts the "this" pointer of the CTestApp class such that when I get back into the OnFileOpen() function the m_nPatID1 = dlg.GetPatID1(); fails.

Looking at the memory allocation somehow the m_nSelectedPatID1 variable and the "this" pointer are using the same memory and hence setting the variable to -1 in the DLL gives a pointer which is nonsense.

Now why is this happening?

I am using two header files for the CLantisPatientSelectDlg. One for build the DLL which needs to know about all the internal stuff and another for building the application which only has the exported functions in it. Reasoning being that the users don't need to know all that internal stuff plus it means they also don't need to have other header files detailing the other classes that the dialog uses to hold it data etc.

So I have
CLantisPatientSelectDlg.h

class AFX_EXT_CLASS CLantisPatientSelectDlg : public CDialog<br />
{<br />
// Construction<br />
public:<br />
	int GetPatID1();<br />
	CLantisPatientSelectDlg(CWnd* pParent = NULL);   // standard constructor<br />
	virtual ~CLantisPatientSelectDlg();   // standard constructor<br />
<br />
// Dialog Data<br />
	//{{AFX_DATA(CLantisPatientSelectDlg)<br />
	enum { IDD = IDD_SELECTPATDLG };<br />
		// NOTE: the ClassWizard will add data members here<br />
	//}}AFX_DATA<br />
<br />
<br />
// Overrides<br />
	// ClassWizard generated virtual function overrides<br />
	//{{AFX_VIRTUAL(CLantisPatientSelectDlg)<br />
	public:<br />
	virtual int DoModal();<br />
	protected:<br />
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support<br />
	//}}AFX_VIRTUAL<br />
<br />
// Implementation<br />
protected:<br />
//	CTypedPtrList<CPtrList, CPatientDetails *> m_lpPatients;<br />
	void DeletePatientList();<br />
	int m_nSelectedPatID1;<br />
	HANDLE m_hListLoadingThread;<br />
//	CPatients m_PatientRecordSet;<br />
	static UINT LoadList(LPVOID pParam);<br />
	bool m_bTerminateLoad;<br />
<br />
	// Generated message map functions<br />
	//{{AFX_MSG(CLantisPatientSelectDlg)<br />
	virtual BOOL OnInitDialog();<br />
	afx_msg void OnDblclkPatientlist(NMHDR* pNMHDR, LRESULT* pResult);<br />
	virtual void OnOK();<br />
	virtual void OnCancel();<br />
	afx_msg void OnSortByName();<br />
	afx_msg void OnSortByNum();<br />
	afx_msg void OnShowAll();<br />
	afx_msg void OnShowActive();<br />
	afx_msg void OnShowOnTX();<br />
	//}}AFX_MSG<br />
	DECLARE_MESSAGE_MAP()<br />
};


and CLantisPatientSelectDlgInt.h

class AFX_EXT_CLASS CLantisPatientSelectDlg : public CDialog<br />
{<br />
// Construction<br />
public:<br />
	int GetPatID1();<br />
	CLantisPatientSelectDlg(CWnd* pParent = NULL);   // standard constructor<br />
<br />
// Overrides<br />
	// ClassWizard generated virtual function overrides<br />
	//{{AFX_VIRTUAL(CLantisPatientSelectDlg)<br />
	public:<br />
	virtual int DoModal();<br />
	//}}AFX_VIRTUAL<br />
};


Now I played around with this this morning and I found that if I add a

protected:<br />
	int m_nSelectedPatID1;


section to the second header file (ie the one for application builders to use) then the application works and both routes out of the dialog work and my "this" pointer doesn't get corrupted.

So have I answered my own question. I'm not sure
- I thought I could use different header files. Can I? If so how do I do it safely.
- why does the memory management go wrong. I have read something about the possibility of the system using two different memory managers that don't know what the other one is doing. How do I check this and what do I do to ensure it is not happening.

Can someone point me to a good guide about this type of DLL construction.

Many thanks and sorry for the long post. If you have got this far I appreciate your endurance.

Andrew Hoole
GeneralRe: DLL memory issue Pin
lynchspawn13-May-05 1:54
lynchspawn13-May-05 1:54 
GeneralRe: DLL memory issue Pin
Andrew Hoole13-May-05 2:39
Andrew Hoole13-May-05 2:39 
GeneralRe: DLL memory issue Pin
Andrew Hoole13-May-05 2:54
Andrew Hoole13-May-05 2:54 
GeneralRe: DLL memory issue Pin
Blake Miller13-May-05 4:03
Blake Miller13-May-05 4:03 
GeneralRe: DLL memory issue Pin
Andrew Hoole13-May-05 5:04
Andrew Hoole13-May-05 5:04 
GeneralRe: DLL memory issue Pin
Blake Miller13-May-05 6:05
Blake Miller13-May-05 6:05 
Generalbyte to hex string Pin
richiemac12-May-05 23:50
richiemac12-May-05 23:50 
GeneralRe: byte to hex string Pin
toxcct12-May-05 23:54
toxcct12-May-05 23:54 
GeneralRe: byte to hex string Pin
richiemac13-May-05 0:13
richiemac13-May-05 0:13 
GeneralRe: byte to hex string Pin
toxcct13-May-05 0:51
toxcct13-May-05 0:51 
GeneralRe: byte to hex string Pin
richiemac13-May-05 1:37
richiemac13-May-05 1:37 
GeneralRe: byte to hex string Pin
David Crow13-May-05 2:15
David Crow13-May-05 2:15 
GeneralRe: byte to hex string Pin
toxcct13-May-05 2:20
toxcct13-May-05 2:20 
GeneralRe: byte to hex string Pin
itkid13-May-05 1:47
itkid13-May-05 1:47 
GeneralRe: byte to hex string Pin
David Crow13-May-05 2:19
David Crow13-May-05 2:19 
GeneralRe: byte to hex string Pin
richiemac13-May-05 3:18
richiemac13-May-05 3:18 
GeneralRe: byte to hex string Pin
David Crow13-May-05 3:26
David Crow13-May-05 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.