Deleting pointer from DLL
<p>ALL,</p>

<p>I have a following issue:</p>

<pre><code>class Base
{
public:
    Base();
    virtual ~Base();
};

class Derived1 : public Base
{
public:
    Derived1();
    virtual Derived1();
};

class Derived2 : public Base
{
public:
    Derived2();
    virtual Derived2();
};
</code></pre>

<p>The base class defined in the DLL, which is statically linked and each child is defined in its own DLL which is loaded on demand.</p>

<pre><code>class Foo
{
public:
    ~Foo();
    void CreateObject();
    void SetPointer(Base *base) { m_p = base; };
private:
    void *m_p;
}

Foo::~Foo()
{
    delete m_p;
    m_p = NULL;
}

extern "C" void CreateObject()
{
     Base *base = NULL;
     Foo *foo = new Foo();
     foo-&gt;CreateObject( base );
     foo-&gt;SetBase( base );
     delete foo;
}

void Foo::CreateObject(Base *m_p)
{
    if( &lt;cond1&gt; )
        m_p = new Derived1();
    if( &lt;cond2&gt; )
        m_p = new Derived2();
}
</code></pre>

<p>Running this code I am getting memory leak. Running thru the debugger, I see that destructor for Derived1 class is never called.</p>

<p>How do I possibly fix it? The destructors are virtuals and should be called. The only issue is that memory allocation is happening inside a DLL but destructor is called inside the main application.</p>

<p>Is it possible to fix a memory leak or I will have to rethink the design?</p>

<p>Thank you.</p>