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. Mobile Development
  3. Mobile
  4. Unable to free memory in the destructor of a class exported in a dll. Pls Help

Unable to free memory in the destructor of a class exported in a dll. Pls Help

Scheduled Pinned Locked Moved Mobile
c++helpperformanceannouncement
5 Posts 2 Posters 13 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.
  • A Offline
    A Offline
    Anand Vinod
    wrote on last edited by
    #1

    Hi Here's what i'm trying; I have a DLL from which I have exported a set of classes. I have 2 generic classes; say CCat and CCats (Simple Classes). In CCats class i have a pointer to an object of the CCat class (CCat *m_pCatsCollection). CCats is supposed to maintain a collection of classes. So in the "Initialize" member function of CCats class i dynamically allocate memory in this fashion; m_pCatsCollection = new CCat[10] // to store 10 cats. Now, in the destructor of the CCats class i'm freeing the memory in this fashion; if(m_pCatsCollection) { delete []m_pCatsCollection; m_pCatsCollection = NULL; } I'm using this DLL to populate contols in a MFC exe! If I put this in the destructor its crashing, and it unpredictable, i.e. sometimes it crashes sometimes it hangs, sometimes it works fine. I even tried freeing individual CCat object memory in a for loop it does not work. I even had a Release member function to do this, but that also doesnt work. Am i doing something wrong, i dont know why this is happenning. Is there any problem in allocating and freeing memory in a Class exported in a DLL. If I comment out the above code of delete then everything works fine, but i know i'm not freeing memory. I also tried using the STL "list" template class to do this, using list::push_back to insert objects and list::clear to delete all objects but still its not working, i'm just not able to free memory in the destructor of the CCats Class. Pls Help! Anand Anand Vinod

    J 1 Reply Last reply
    0
    • A Anand Vinod

      Hi Here's what i'm trying; I have a DLL from which I have exported a set of classes. I have 2 generic classes; say CCat and CCats (Simple Classes). In CCats class i have a pointer to an object of the CCat class (CCat *m_pCatsCollection). CCats is supposed to maintain a collection of classes. So in the "Initialize" member function of CCats class i dynamically allocate memory in this fashion; m_pCatsCollection = new CCat[10] // to store 10 cats. Now, in the destructor of the CCats class i'm freeing the memory in this fashion; if(m_pCatsCollection) { delete []m_pCatsCollection; m_pCatsCollection = NULL; } I'm using this DLL to populate contols in a MFC exe! If I put this in the destructor its crashing, and it unpredictable, i.e. sometimes it crashes sometimes it hangs, sometimes it works fine. I even tried freeing individual CCat object memory in a for loop it does not work. I even had a Release member function to do this, but that also doesnt work. Am i doing something wrong, i dont know why this is happenning. Is there any problem in allocating and freeing memory in a Class exported in a DLL. If I comment out the above code of delete then everything works fine, but i know i'm not freeing memory. I also tried using the STL "list" template class to do this, using list::push_back to insert objects and list::clear to delete all objects but still its not working, i'm just not able to free memory in the destructor of the CCats Class. Pls Help! Anand Anand Vinod

      J Offline
      J Offline
      Joao Paulo Figueira
      wrote on last edited by
      #2

      Anand Vinod wrote: I'm using this DLL to populate contols in a MFC exe Did you create this DLL as an MFC extension DLL? I never experienced such a thing, but all my DLLs are created as MFC extensions...

      A 1 Reply Last reply
      0
      • J Joao Paulo Figueira

        Anand Vinod wrote: I'm using this DLL to populate contols in a MFC exe Did you create this DLL as an MFC extension DLL? I never experienced such a thing, but all my DLLs are created as MFC extensions...

        A Offline
        A Offline
        Anand Vinod
        wrote on last edited by
        #3

        Hi João, Thanks for the reply. I've not made my Dll an MFC extension DLL since i'm not using any MFC classes in the dll. But shud this cause a problem. Anand

        J 2 Replies Last reply
        0
        • A Anand Vinod

          Hi João, Thanks for the reply. I've not made my Dll an MFC extension DLL since i'm not using any MFC classes in the dll. But shud this cause a problem. Anand

          J Offline
          J Offline
          Joao Paulo Figueira
          wrote on last edited by
          #4

          Hi Anand, Looking at your code, I could see nothing wrong. I have written code for DLLs where such allocations and deallocations were being made and I never had a similar problem. I suspect (never checked it) that MFC may use its own operator new and, by not using an MFC extension DLL, you might be exposing yourself to an allocation nightmare. I would suggest to try this code in an MFC extension DLL and check the results. That's my best guess so far. Good luck! João Paulo

          1 Reply Last reply
          0
          • A Anand Vinod

            Hi João, Thanks for the reply. I've not made my Dll an MFC extension DLL since i'm not using any MFC classes in the dll. But shud this cause a problem. Anand

            J Offline
            J Offline
            Joao Paulo Figueira
            wrote on last edited by
            #5

            Well, I think my recent post might actually be a good starting point. You see, MFC does redefine operator new in afxmem.cpp. Looking at the code you can see that there are special provisions for when you are using MFC in a shared DLL. As a matter of fact, operator new is implemented in the MFC DLL (if you are using it at all). So this might be the source of your problems: your app is using one operator new and your DLL is using another... :eek:

            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