CCed the whole group and correct one mistake. Best regards, Wallace Wang(WANG Zhi-Peng) -----Original Message----- From: Wallace Wang Sent: Thursday, May 21, 2009 14:18 To: me22 Subject: RE: Wrong delete called for exported class that has virtual destructor. Hi Scott, I am afraid this is not a problem of transferring ownership between dlls. An example explains more clearly. The article of MS support also tells the cause of this issue very well( http://support.microsoft.com/kb/122675). In one DLL I had: void* operator new(siz_t size) { cout<<"DLL new"<<endl; return malloc(size); } void operator delete(void* p) { cout<<"DLL delete"<<endl; free(p); } class DLLClass { public: virtual ~DLLClass() {} } And in an EXE that uses the DLL, I had: void* operator new(size_t size) { cout<<"EXE new"<<endl; return malloc(size); } void operator delete(void* p) { cout<<"EXE delete"<<endl; free(p); } int main() { Test* t = new Test(); delete t; } The EXE new operator is called to allocate t while the DLL delete is called to delete t. Best regards, Wallace Wang(WANG Zhi-Peng) -----Original Message----- From: me22 [mailto:me22.ca@xxxxxxxxx] Sent: Wednesday, May 20, 2009 12:13 To: Wallace Wang Cc: gcc-help@xxxxxxxxxxx Subject: Re: Wrong delete called for exported class that has virtual destructor. 2009/5/19 Wallace Wang <Wallace.Wang@xxxxxxxxxxxx>: > > 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. > > 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? > If you only pass a shared_ptr out of the DLL, instead of a raw one, that shared_ptr keeps a pointer to the DLL's delete, so no matter where it ends up getting deleted the correct delete gets called. That has all sorts of other consequences, so YMMV. ~ Scott