I have code that runs on Windows. It works great on many unix platforms, and the leak only occurs on Windows. The binary file consists of exe, 1 dll and 2 static libs. Exe refers to both dll and static libs, and static libs also refer to DLLs. A leak occurs in exe code, when instead of calling a vector that removes the destructor, scalar destructor removal is called for some reason. This leads to the fact that only the first object in the array will be deleted, and the rest of the array will remain in memory.
Penetrating pseudocode is as follows:
class MyClassFromExe : public MyBaseClassFromDll { public: ClassFromDll* m_arr; MyClassFromExe(unsigned int size) { m_arr = new ClassFromDll[size]; } ~MyClassFromExe() { delete [] m_arr; } }; void func() { MyClassFromExe obj(3); }
When func () ends and the destructor is called, I see that only the destructor of the first object in m_arr is called. From the debugger, I see that this is done from a scalar destructor deletion, and not from a vector destructor deletion. This explains why only the first object is destroyed. What I need to understand is why scalar destructor deletion is called when using delete [] ???
I found this thread - Why is a vector removing a destructor called as a result of scalar deletion? . I followed the suggestions and made sure that all modules were compiled using / MD.
It is important to note that when the DLL containing ClassFromDll was a static library, not a dll, everything worked fine. The leak started only when the static library was changed as a DLL. While the program runs in Release mode, it shuts down in debug mode when [] m_arr is deleted. The crash occurs on the line dbgdel.cpp 52 - _BLOCK_TYPE_IS_VALID (pHead-> nBlockUse).
On unix platforms, this lib is also a shared lib library and is expected to invoke a vector destructive destructor there, and there is no leak. Could the problem be with the VC compiler? Or maybe some other project settings need to be changed? I am using VC2003.
Thank you in advance!
c ++ debugging vector memory-leaks destructor
noplk
source share