Will this cause a problem with different runtimes with DLL?

Posted by Milo on Stack Overflow See other posts from Stack Overflow or by Milo
Published on 2011-01-10T15:39:25Z Indexed on 2011/01/10 15:54 UTC
Read the original article Hit count: 172

Filed under:
|

My gui application supports polymorphic timed events so that means that the user calls new, and the gui calls delete. This can create a problem if the runtimes are incompatible.

So I was told a proposed solution would be this:

class base;

class Deallocator {
    void operator()(base* ptr) 
    {
        delete ptr;
    }
}

class base {
public:
base(Deallocator dealloc) 
{
    m_deleteFunc = dealloc;
}
~base() 
{
    m_deleteFunc(this);
}

private:
Deallocator m_deleteFunc;
}

int main
{
    Deallocator deletefunc;

    base baseObj(deletefunc);
}

While this is a good solution, it does demand that the user create a Deallocator object which I do not want. I was however wondering if I provided a Deallocator to each derived class: eg

class derived : public base
{
  Deallocator dealloc;
public:
  Derived() : base(dealloc);
{
}
};

I think this still does not work though. The constraint is that: The addTimedEvent() function is part of the Widget class which is also in the dll, but it is instanced by the user. The other constraint is that some classes which derive from Widget call this function with their own timed event classes.

Given that "he who called new must call delete" what could work given these constraints?

Thanks

© Stack Overflow or respective owner

Related posts about c++

Related posts about dll