Virtual destructor causes access violation

I am trying to make a DLL file compatible with various compiler configurations (Debug, Release, ..). To make sure the object was deleted correctly, I was able to write a pointer wrapper class that uses the compiled delete operator whenever I take a DLL object and end out of scope.

I do a great job of this, but my program crashes when I try to delete the memory that I allocated in the same method / program.

Here is an example of code compiled in standard Release mode:

heading

template <typename T>
class API mwCompatibleObject
{
public:

    //! Constructor
    mwCompatibleObject();

    //! Destructor
    virtual ~mwCompatibleObject();
};

source

template < typename T >
mwCompatibleObject< T >::mwCompatibleObject() {}

template <typename T>
mwCompatibleObject<T>::~mwCompatibleObject() {}

Note. The API is defined as export / import.

Now I use this same class in the debug mode application, where I create an instance and delete it right away.

mwCompatibleObject<double> * obj = new mwCompatibleObject<double>();
delete obj;

Executing the delete statement gives an access violation on mlock.c 376.

-:

ntdll.dll!7721e3be()    
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
msvcr80d.dll!_unlock(int locknum=4)  Line 376   C
msvcr80d.dll!_heap_alloc_dbg(unsigned int nSize=0, int nBlockUse=2968120, const char * szFileName=0x2e2ed26c, int nLine=1638180)  Line 477 + 0x7 bytes  C++
msvcr80d.dll!_heap_alloc_dbg(unsigned int nSize=0, int nBlockUse=2968120, const char * szFileName=0x2e2ed26c, int nLine=1638180)  Line 474 + 0xc bytes  C++
00300000()  
msvcr80d.dll!malloc(unsigned int nSize=2968120)  Line 154 + 0x15 bytes  C++
5axutil.dll!100b5d09()  
Integrator3.exe!main()  Line 54 + 0x34 bytes    C++

- , , , .

/ DLL-?

+3
1

, , . - , .

, ++ new delete malloc free C, , .

Edit:

, . , , . vtable ?

template <typename T>
class mwCompatibleObject // no need for export if inlined in header
{
public:
    //! Constructor
    mwCompatibleObject() {}
    //! Destructor (don't need virtual unless it a base class or has virtual methods)
    ~mwCompatibleObject() {}

    //! Some public method
    void DoSomething(const T& withSomething)
    {
        // ... yata yata
    }
private:
    T m_member;
};

:

, ( , ) ++, . , , . . Herb Sutter ++ 0x

+4

All Articles