Memory Leak
-
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
-
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
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++) -
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
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!
-
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!
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
-
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++)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. :-)
-
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. :-)
:thumbsup:
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)