Unable to free memory in the destructor of a class exported in a dll. Pls Help
-
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
-
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
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...
-
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...
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
-
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
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 -
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
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 oneoperator new
and your DLL is using another... :eek: