Wrong delete called for exported class that has virtual destructor.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

This should be a known issue. However, I have been searching with Google but got no perfect answers. 

Symptoms:
When allocating and deleting an object of a class that is exported from a DLL, you may find that the new operator in the EXE is called to allocate the memory but the delete operator in the DLL is called to delete the memory. Therefore, there is a new/delete mismatching problem, which may cause run-time errors.

The old VC++ compiler also has such problem, but MS solved this issue in its newer compiler. If a class is declared as __declspec(dllimport) and has a virtual destructor, the compiler will create a new set of virtual tables when an object of that class is created using new. The compiler substitutes the virtual destructor method, with one that uses the delete method in the same DLL that created the object. This helps make memory management consistent for these classes.

More information about this issue can be found from below article.
http://support.microsoft.com/kb/122675

I don't like the idea to override the new/delete operators in all of my classes. It is a huge change and may require multiple inheritance. Any other suggestion? 

Thanks,
Wallace Wang(WANG Zhi-Peng)



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux