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. Memory Leak

Memory Leak

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studioperformancequestion
6 Posts 3 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.
  • B Offline
    B Offline
    BarryPearlman
    wrote on last edited by
    #1

    I am attempting to write an application in VC 2008 under Vista 64. In the application, I create a Class and a pointer; the pointer is cast (void) and held in a map. <CMap<int, int, void*, void*> myMap m_pClassXYZ = new ClassXYZ; m_pClassXYZ->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), this, nClsID, NULL); p_vPtr = (void*)m_pClassXYZ; //Note: Map holds pointers to multiple classes of different types and hence is cast void. pmyMap->SetAt(n, p_vPtr); When I want to use the pointer, it is cast back: m_pClassXYZ = (ClassXYZ*)p_vPtr; This seems to compile, link and execute without incident. On exit the application I want to destroy the Class(es) and free the memory: m_pClassXYZ->DestroyWindow(); delete m_pClassXYZ; The appropriate DLL's are part of the project and allow memory leak checking. I get the following in the Output pane when the application exits: c:\..................\MyApp.cpp(164) : {192} client block at 0x00098F60, subtype c0, 88 bytes long. a ClassXYZ object at $00098F60, 88 bytes long When I click on the leak statement, it takes me to the line with the "new" operator. I understand the leak statement, just not why. I think that this worked in previous versions of Visual Studio. Am I not destroying the class and freeing memory properly? If not, can someone please give me an idea of what I am doing wrong? Thanks, Barry

    _ P 2 Replies Last reply
    0
    • B BarryPearlman

      I am attempting to write an application in VC 2008 under Vista 64. In the application, I create a Class and a pointer; the pointer is cast (void) and held in a map. <CMap<int, int, void*, void*> myMap m_pClassXYZ = new ClassXYZ; m_pClassXYZ->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), this, nClsID, NULL); p_vPtr = (void*)m_pClassXYZ; //Note: Map holds pointers to multiple classes of different types and hence is cast void. pmyMap->SetAt(n, p_vPtr); When I want to use the pointer, it is cast back: m_pClassXYZ = (ClassXYZ*)p_vPtr; This seems to compile, link and execute without incident. On exit the application I want to destroy the Class(es) and free the memory: m_pClassXYZ->DestroyWindow(); delete m_pClassXYZ; The appropriate DLL's are part of the project and allow memory leak checking. I get the following in the Output pane when the application exits: c:\..................\MyApp.cpp(164) : {192} client block at 0x00098F60, subtype c0, 88 bytes long. a ClassXYZ object at $00098F60, 88 bytes long When I click on the leak statement, it takes me to the line with the "new" operator. I understand the leak statement, just not why. I think that this worked in previous versions of Visual Studio. Am I not destroying the class and freeing memory properly? If not, can someone please give me an idea of what I am doing wrong? Thanks, Barry

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      This how I would do the allocation into CMap.

      CMap<int, int, void*, void*> myMap;

      m_pClassXYZ = new ClassXYZ;
      m_pClassXYZ->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), this, nClsID, NULL);
      p_vPtr = (void*)m_pClassXYZ;

      myMap.SetAt(n, p_vPtr);

      The deallocation would be done as follows -

      int nKey;
      void* pValue;
      POSITION pos = myMap.GetStartPosition();
      while (pos)
      {
      myMap.GetNextAssoc(&pos, &nKey, &pValue);
      myMap.RemoveKey(nKey); // Remove from the map.
      m_pClassXYZ = (ClassXYZ*)pValue;
      delete m_pClassXYZ; // Deallocate memory.
      }

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      B 1 Reply Last reply
      0
      • B BarryPearlman

        I am attempting to write an application in VC 2008 under Vista 64. In the application, I create a Class and a pointer; the pointer is cast (void) and held in a map. <CMap<int, int, void*, void*> myMap m_pClassXYZ = new ClassXYZ; m_pClassXYZ->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), this, nClsID, NULL); p_vPtr = (void*)m_pClassXYZ; //Note: Map holds pointers to multiple classes of different types and hence is cast void. pmyMap->SetAt(n, p_vPtr); When I want to use the pointer, it is cast back: m_pClassXYZ = (ClassXYZ*)p_vPtr; This seems to compile, link and execute without incident. On exit the application I want to destroy the Class(es) and free the memory: m_pClassXYZ->DestroyWindow(); delete m_pClassXYZ; The appropriate DLL's are part of the project and allow memory leak checking. I get the following in the Output pane when the application exits: c:\..................\MyApp.cpp(164) : {192} client block at 0x00098F60, subtype c0, 88 bytes long. a ClassXYZ object at $00098F60, 88 bytes long When I click on the leak statement, it takes me to the line with the "new" operator. I understand the leak statement, just not why. I think that this worked in previous versions of Visual Studio. Am I not destroying the class and freeing memory properly? If not, can someone please give me an idea of what I am doing wrong? Thanks, Barry

        P Offline
        P Offline
        PJ Arends
        wrote on last edited by
        #3

        You say you are using DLLs. Is the memory allocated in one module and deleted in another? You have to keep the allocation and deletion of objects in the same module.


        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

        B 1 Reply Last reply
        0
        • P PJ Arends

          You say you are using DLLs. Is the memory allocated in one module and deleted in another? You have to keep the allocation and deletion of objects in the same module.


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          B Offline
          B Offline
          BarryPearlman
          wrote on last edited by
          #4

          My mistake. I was refering to the header files MS supplies that allow the memory leak detail in the output pane. The project does not have any DLL's as of this stage in the writing. Again, sorry for misleading you. Thanks, Barry

          1 Reply Last reply
          0
          • _ _Superman_

            This how I would do the allocation into CMap.

            CMap<int, int, void*, void*> myMap;

            m_pClassXYZ = new ClassXYZ;
            m_pClassXYZ->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), this, nClsID, NULL);
            p_vPtr = (void*)m_pClassXYZ;

            myMap.SetAt(n, p_vPtr);

            The deallocation would be done as follows -

            int nKey;
            void* pValue;
            POSITION pos = myMap.GetStartPosition();
            while (pos)
            {
            myMap.GetNextAssoc(&pos, &nKey, &pValue);
            myMap.RemoveKey(nKey); // Remove from the map.
            m_pClassXYZ = (ClassXYZ*)pValue;
            delete m_pClassXYZ; // Deallocate memory.
            }

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            B Offline
            B Offline
            BarryPearlman
            wrote on last edited by
            #5

            Superman, you came to the rescue. Your reply cleared out all of those pesky client blocks. Thanks. I do have some normal blocks left, but I will start another thread to address them. Barry BTW - I too love work; I can lie down next to it all day long. :-)

            _ 1 Reply Last reply
            0
            • B BarryPearlman

              Superman, you came to the rescue. Your reply cleared out all of those pesky client blocks. Thanks. I do have some normal blocks left, but I will start another thread to address them. Barry BTW - I too love work; I can lie down next to it all day long. :-)

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              :thumbsup:

              «_Superman_» I love work. It gives me something to do between weekends.
              Microsoft MVP (Visual C++)

              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