Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. vector and delete

vector and delete

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ns
    wrote on last edited by
    #1

    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

    V 1 Reply Last reply
    0
    • N 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

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      How does the first solution not work? Kuphryn

      N 1 Reply Last reply
      0
      • V valikac

        How does the first solution not work? Kuphryn

        N Offline
        N Offline
        ns
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • N 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.

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          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?!?

          N 1 Reply Last reply
          0
          • J jhwurmbach

            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?!?

            N Offline
            N Offline
            ns
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • N 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

              T Offline
              T Offline
              TFrancis
              wrote on last edited by
              #6

              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

              N 1 Reply Last reply
              0
              • T TFrancis

                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

                N Offline
                N Offline
                ns
                wrote on last edited by
                #7

                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

                S 1 Reply Last reply
                0
                • N 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

                  S Offline
                  S Offline
                  souldog
                  wrote on last edited by
                  #8

                  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.

                  N 1 Reply Last reply
                  0
                  • S souldog

                    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.

                    N Offline
                    N Offline
                    ns
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups