vector and delete
-
I have a cDialog class that I spawn with new MyClass in another class (MainClass), and I push_back this m_pMyClass pointer on a vector. Now from another location I wanted to delete and zero out the pointer and I tried:
delete myGlobalVector\[i\]; myGlobalVector\[i\]= 0;
This didnt seem to do it. So then I did,
delete MainClass.m_pMyClass;
MainClass.m_pMyClass = 0;
and this worked. I am puzzled. I thought the first approach would work too..............??:confused: Thanks, ns
-
I have a cDialog class that I spawn with new MyClass in another class (MainClass), and I push_back this m_pMyClass pointer on a vector. Now from another location I wanted to delete and zero out the pointer and I tried:
delete myGlobalVector\[i\]; myGlobalVector\[i\]= 0;
This didnt seem to do it. So then I did,
delete MainClass.m_pMyClass;
MainClass.m_pMyClass = 0;
and this worked. I am puzzled. I thought the first approach would work too..............??:confused: Thanks, ns
-
-
After I do the delete and NULLing I have a test: if (!m_pMyCLass)m_button.EnableWindow(TRUE); but my button doesnt get enabled if I delete it the first (vector) way. The debuuger tells me that indeed the pointer is not zero at this point! Thanks for helping.
ns wrote: if (!m_pMyCLass)m_button.EnableWindow(TRUE); Please show your actual code. What you did write here does not use your vector at all!
Who is 'General Failure'? And why is he reading my harddisk?!?
-
ns wrote: if (!m_pMyCLass)m_button.EnableWindow(TRUE); Please show your actual code. What you did write here does not use your vector at all!
Who is 'General Failure'? And why is he reading my harddisk?!?
I push_back m_pDLGSettings onto the vector. Then I get rid of it as below . Then I test it: void CImageDisplay::OnBsettings() { m_pDlgSettings = new CDlgSettings ; if (m_pDlgSettings == NULL) return; BOOL ret = m_pDlgSettings->Create(IDD_DLGSETTINGS, this); m_pDlgSettings->ShowWindow(SW_SHOW); pDlgSettingsVector.push_back(m_pDlgSettings); m_bSettings.EnableWindow(FALSE); } In another place where I want all windows to destroy and zero out:
if( pDlgSettingsVector.size()) { for (int i =0; i < pDlgSettingsVector.size(); i++) { pDlgSettingsVector\[i\]->DestroyWindow(); delete pDlgSettingsVector\[i\]; pDlgSettingsVector\[i\]=0; } pDlgSettingsVector.clear(); }
and in a function of the class that the index [i] refers to:
if (!m\_pDlgSettings) { m\_bSettings.EnableWindow(TRUE); }
I didnt use the vector here because I dont have the location of the pointer in that vector at this point in code. Instead I access it directly, feeling assured that Ithough deleted it using an index, I can always use it by its name instead of its position in the vector. (m_pDLgSettings is a member). In the place I want to check it (last snippet) I am not using its vector position but its actual variable that I pushed onto the vector.......... Hope I am not hopelessly unclear.... :) Thanks, ns
-
I push_back m_pDLGSettings onto the vector. Then I get rid of it as below . Then I test it: void CImageDisplay::OnBsettings() { m_pDlgSettings = new CDlgSettings ; if (m_pDlgSettings == NULL) return; BOOL ret = m_pDlgSettings->Create(IDD_DLGSETTINGS, this); m_pDlgSettings->ShowWindow(SW_SHOW); pDlgSettingsVector.push_back(m_pDlgSettings); m_bSettings.EnableWindow(FALSE); } In another place where I want all windows to destroy and zero out:
if( pDlgSettingsVector.size()) { for (int i =0; i < pDlgSettingsVector.size(); i++) { pDlgSettingsVector\[i\]->DestroyWindow(); delete pDlgSettingsVector\[i\]; pDlgSettingsVector\[i\]=0; } pDlgSettingsVector.clear(); }
and in a function of the class that the index [i] refers to:
if (!m\_pDlgSettings) { m\_bSettings.EnableWindow(TRUE); }
I didnt use the vector here because I dont have the location of the pointer in that vector at this point in code. Instead I access it directly, feeling assured that Ithough deleted it using an index, I can always use it by its name instead of its position in the vector. (m_pDLgSettings is a member). In the place I want to check it (last snippet) I am not using its vector position but its actual variable that I pushed onto the vector.......... Hope I am not hopelessly unclear.... :) Thanks, ns
The item in the vector is actually a copy of the pointer. In other words, you have two pointers to the CDlgSettings object, m_pDlgSettings and pDlgSettingsVector[0]. While push_back() does pass the object by reference, it makes a copy of the object (in this case a CDlgSettings*) to place in the vector. Thus you are setting one pointer to the CDlgSettigns object to NULL, but the other one still points to the now deleted object. Possible work around - make the vector contain pointers to pointers, thus the vector will contain a pointer to m_pDlgSettings. Hope this helps, Tim
-
The item in the vector is actually a copy of the pointer. In other words, you have two pointers to the CDlgSettings object, m_pDlgSettings and pDlgSettingsVector[0]. While push_back() does pass the object by reference, it makes a copy of the object (in this case a CDlgSettings*) to place in the vector. Thus you are setting one pointer to the CDlgSettigns object to NULL, but the other one still points to the now deleted object. Possible work around - make the vector contain pointers to pointers, thus the vector will contain a pointer to m_pDlgSettings. Hope this helps, Tim
Thanks!!! Thats a subtle point I was unaware of. Appreciate the workaround suggestion. I am trying it out. Not quite sure what I'll be deleting if I delete the vector element. Its a pointer to a pointer so do I delete *(pDlgSettingsVector[i]) ? i.e. the contents of the pointer contained in the vector. Thanks, ns
-
Thanks!!! Thats a subtle point I was unaware of. Appreciate the workaround suggestion. I am trying it out. Not quite sure what I'll be deleting if I delete the vector element. Its a pointer to a pointer so do I delete *(pDlgSettingsVector[i]) ? i.e. the contents of the pointer contained in the vector. Thanks, ns
No, No, No.. Don't do this. Do not store the pointer returned by new anywhere else except the vector. All access to that object should be through the pointer stored in the vector. Your problem here is that you have many copies of the pointers floating around. You need to redesign your application so that this is not the case. if the code is inside the class pointed to then it should not be possible to call it in any other way except through the pointer stored in the vector, so you don't need to check it for NULL This is how it should be done. ANy other way and you have Code that is very hard to maintain and very error prone. Can you explain why and how you are possibly calling a function in a class the instantiation of has just been deleted from memory.
-
No, No, No.. Don't do this. Do not store the pointer returned by new anywhere else except the vector. All access to that object should be through the pointer stored in the vector. Your problem here is that you have many copies of the pointers floating around. You need to redesign your application so that this is not the case. if the code is inside the class pointed to then it should not be possible to call it in any other way except through the pointer stored in the vector, so you don't need to check it for NULL This is how it should be done. ANy other way and you have Code that is very hard to maintain and very error prone. Can you explain why and how you are possibly calling a function in a class the instantiation of has just been deleted from memory.
Thanks. To clarify, if I understand your ending question , I needed to see in some other function whether any windows of the sort I pushed onto the vector exist. REgarding the prog structure, I later realized that I didnt need a global vector (dangerous anyways) since I was always able to get the object that had the window as a member, so I could iterate trough the parent windows and get t o the member windows (which I had been poushing back) through the pointers to the parents. MAny thanks for the help! ns