DLL and CTypedPtrList problem
-
Hi All I have run out of ideas can anyone help me. I have created a extension DLL for a Patient selection dialog. When the dialog opens it initializes a list with with patient details. The user can then select one of the patients and the dialog closes and then the program can query the dialog before its destruction to get the index of the patient who was selected. void CTestApp::OnFileOpen() { CLantisPatientSelectDlg dlg; if(dlg.DoModal() == IDOK){ m_nPatID1 = dlg.GetPatID1(); } } To try and be tidy in the OnOK() member function of my class derived from CDialog I delete all of the list. void CLantisPatientSelectDlg::OnOK() { int nIndex; CPatientDetails *pPatientDetails; CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_PATIENTLIST); nIndex = pList->GetSelectionMark(); if(nIndex != -1){ POSITION pos = m_lpPatients.FindIndex(nIndex); if(pos != NULL){ pPatientDetails = m_lpPatients.GetAt(pos); m_nSelectedPatID1 = pPatientDetails->m_Pat_ID1; } } else { m_nSelectedPatID1 = -1; } // Get any background list loading to terminate m_bTerminateLoad = true; ::WaitForSingleObject(m_hListLoadingThread,INFINITE); DeletePatientList(); CDialog::OnOK(); } void CLantisPatientSelectDlg::DeletePatientList() { CPatientDetails *pPatientDetails; POSITION pos = m_lpPatients.GetHeadPosition(); while(pos != NULL){ pPatientDetails = m_lpPatients.RemoveHead(); delete pPatientDetails; pos = m_lpPatients.GetHeadPosition(); } } Now all of this seems to work alright until it comes to the end of the OnFileOpen() function at which point the instance of the dialog goes out of scope and the system deletes it. At this point the list which was cleared before and had 0 elements now seems to have a count of -1 (0xFFFFFFFF) and the routine to get rid of it crashes. I am using a different header file for the DLL'ed Dialog in the application which only lists the functions that have been exported. Any ideas??
-
Hi All I have run out of ideas can anyone help me. I have created a extension DLL for a Patient selection dialog. When the dialog opens it initializes a list with with patient details. The user can then select one of the patients and the dialog closes and then the program can query the dialog before its destruction to get the index of the patient who was selected. void CTestApp::OnFileOpen() { CLantisPatientSelectDlg dlg; if(dlg.DoModal() == IDOK){ m_nPatID1 = dlg.GetPatID1(); } } To try and be tidy in the OnOK() member function of my class derived from CDialog I delete all of the list. void CLantisPatientSelectDlg::OnOK() { int nIndex; CPatientDetails *pPatientDetails; CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_PATIENTLIST); nIndex = pList->GetSelectionMark(); if(nIndex != -1){ POSITION pos = m_lpPatients.FindIndex(nIndex); if(pos != NULL){ pPatientDetails = m_lpPatients.GetAt(pos); m_nSelectedPatID1 = pPatientDetails->m_Pat_ID1; } } else { m_nSelectedPatID1 = -1; } // Get any background list loading to terminate m_bTerminateLoad = true; ::WaitForSingleObject(m_hListLoadingThread,INFINITE); DeletePatientList(); CDialog::OnOK(); } void CLantisPatientSelectDlg::DeletePatientList() { CPatientDetails *pPatientDetails; POSITION pos = m_lpPatients.GetHeadPosition(); while(pos != NULL){ pPatientDetails = m_lpPatients.RemoveHead(); delete pPatientDetails; pos = m_lpPatients.GetHeadPosition(); } } Now all of this seems to work alright until it comes to the end of the OnFileOpen() function at which point the instance of the dialog goes out of scope and the system deletes it. At this point the list which was cleared before and had 0 elements now seems to have a count of -1 (0xFFFFFFFF) and the routine to get rid of it crashes. I am using a different header file for the DLL'ed Dialog in the application which only lists the functions that have been exported. Any ideas??
Delete the
m_lpPatients
list in the dialog's destructor. That way it gets cleaned up no matter if the OK, Cancel, or X button is used.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Delete the
m_lpPatients
list in the dialog's destructor. That way it gets cleaned up no matter if the OK, Cancel, or X button is used.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Yeah I agree with you and I was doing that but it doesn't seem to be the problem. When it comes time to destruct the instance of the dialog the list, which is empty by this time, appears to have been corrupted in some way and hence can't delete properly. I have just noticed something else. In the code section void CTestApp::OnFileOpen() { CLantisPatientSelectDlg dlg; if(dlg.DoModal() == IDOK){ m_nPatID1 = dlg.GetPatID1(); } } In my testing before posting I didn't have the m_nPatID1 = dlg.GetPatID1(); line and hence my code went on to destruct the dlg instance immediately. Now with this code a new failure mode is occurring along with the old one which is generated when you Cancel out of the dialog. In the line highlighted above the dlg.GetPatID1() function runs correctly and returns an int but an access violation occurs when this return value is copied to the m_nPatID1 variable. I must be doing something silly. I just can't see it. Andrew
-
Hi All I have run out of ideas can anyone help me. I have created a extension DLL for a Patient selection dialog. When the dialog opens it initializes a list with with patient details. The user can then select one of the patients and the dialog closes and then the program can query the dialog before its destruction to get the index of the patient who was selected. void CTestApp::OnFileOpen() { CLantisPatientSelectDlg dlg; if(dlg.DoModal() == IDOK){ m_nPatID1 = dlg.GetPatID1(); } } To try and be tidy in the OnOK() member function of my class derived from CDialog I delete all of the list. void CLantisPatientSelectDlg::OnOK() { int nIndex; CPatientDetails *pPatientDetails; CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_PATIENTLIST); nIndex = pList->GetSelectionMark(); if(nIndex != -1){ POSITION pos = m_lpPatients.FindIndex(nIndex); if(pos != NULL){ pPatientDetails = m_lpPatients.GetAt(pos); m_nSelectedPatID1 = pPatientDetails->m_Pat_ID1; } } else { m_nSelectedPatID1 = -1; } // Get any background list loading to terminate m_bTerminateLoad = true; ::WaitForSingleObject(m_hListLoadingThread,INFINITE); DeletePatientList(); CDialog::OnOK(); } void CLantisPatientSelectDlg::DeletePatientList() { CPatientDetails *pPatientDetails; POSITION pos = m_lpPatients.GetHeadPosition(); while(pos != NULL){ pPatientDetails = m_lpPatients.RemoveHead(); delete pPatientDetails; pos = m_lpPatients.GetHeadPosition(); } } Now all of this seems to work alright until it comes to the end of the OnFileOpen() function at which point the instance of the dialog goes out of scope and the system deletes it. At this point the list which was cleared before and had 0 elements now seems to have a count of -1 (0xFFFFFFFF) and the routine to get rid of it crashes. I am using a different header file for the DLL'ed Dialog in the application which only lists the functions that have been exported. Any ideas??
Andrew Hoole wrote: Now all of this seems to work alright until it comes to the end of the OnFileOpen() function at which point the instance of the dialog goes out of scope and the system deletes it. At this point the list which was cleared before and had 0 elements now seems to have a count of -1 (0xFFFFFFFF) and the routine to get rid of it crashes. Do you also iterate through the list and delete items when the dialog is destroyed? It looks like you only do this when clicking OK so I'm confused about the part you wrote above stating that the system deletes the dialog and something happens to the list. What does your GetPatID1() function do - just return the value of the int found during the OnOK() function or does it make use of the list also? Does anything else access your list? One last thought - is that style of iteration safe? I don't use lists very often and haven't seen that style of iteration before. I got so frustrated using lists recently (a similar crashing issue that I just couldn't figure out)- I just ended up going with a CTypedPtryArray. I know this won't always work but in my case, the list wasn't really needed and the array worked just fine (and I was able to move on). If you do figure it out, please post what you found. Good luck!
-
Andrew Hoole wrote: Now all of this seems to work alright until it comes to the end of the OnFileOpen() function at which point the instance of the dialog goes out of scope and the system deletes it. At this point the list which was cleared before and had 0 elements now seems to have a count of -1 (0xFFFFFFFF) and the routine to get rid of it crashes. Do you also iterate through the list and delete items when the dialog is destroyed? It looks like you only do this when clicking OK so I'm confused about the part you wrote above stating that the system deletes the dialog and something happens to the list. What does your GetPatID1() function do - just return the value of the int found during the OnOK() function or does it make use of the list also? Does anything else access your list? One last thought - is that style of iteration safe? I don't use lists very often and haven't seen that style of iteration before. I got so frustrated using lists recently (a similar crashing issue that I just couldn't figure out)- I just ended up going with a CTypedPtryArray. I know this won't always work but in my case, the list wasn't really needed and the array worked just fine (and I was able to move on). If you do figure it out, please post what you found. Good luck!
Hi I'm stilling working on this but have started a new post as the topic seems to have move on a bit. See DLL memory issue[^] post. Andrew Hoole