I dynamically load a library in C++ like described here.
My abstract base class looks like this:
#include <boost/ptr_container/ptr_list.hpp>
class Base {
public:
virtual void get_list(boost::ptr_list<AnotherObject>& list) const = 0;
};
And my library now provides a derived class Derived
class Derived : public Base { ... };
void Derived::get_list(boost::ptr_list<AnotherObject& list) const {
list.push_back(new AnotherObject(1));
list.push_back(new AnotherObject(2));
}
and create and destroy functions
extern "C" {
Base* create() { new Derived; }
destroy(Base* p) { delete p; }
}
My client program loads the library and the two create and destroy functions. Then it creates an instance of Derived and uses it:
Base* obj = create();
boost::ptr_list<AnotherObject> list;
obj->get_list(list);
Now my problem: When the list is filled by the library the library's new is called to create the AnotherObjects. On the other hand when the list is destroyed the client's delete is called for destroying the AnotherObjects. What can I do to avoid this problem?