ALOR
ALOR

Reputation: 545

Pure virtual destructor in interface

Here is my problem: I'm making a C++ DLL which relies extensively on instance object exports. So I return my actual instances as a pointers to an interface through some exported factory method.

The interfaces I use are purely virtual, to avoid linking problems. So I need a pure virtual destructor too, and I implemented one (with empty body, as I googled it). All compiles perfectly well, except... I can't see if the actual destructors are called or not - because when I added some

std::cout << "hello destructor";
I never get to see it.

I have some explicit "delete obj" (EDIT: and it is called from the "FreeObject" method inside the DLL), that's not the problem.

Am I missing something? Is there another way to delete my object through an interface?

EDIT: Again, I don't have memory management inconsistency, it's all inside the DLL. But the right destructor just isn't called.

Upvotes: 1

Views: 3802

Answers (3)

Hayman
Hayman

Reputation: 185

This is how I solved your particular problem for dynamically loaded C++ classes -

Have a base class for all pluggable objects, ie

class object_t {

 public:
  virtual ~object_t();

 //other type utils...

};

Have an base interface for all plugin interfaces, ie

class object_t;

class interface_t {

 public:
  virtual object_t* object() = 0;

}

object_t will have linkage, define it in its own dll that you will link against plugin classes. Other useful hooks to have in object_t are copying, spawning, RTTI and other type utils, my base object has spawn(), copy(), object_name() for example.

So all concrete classes derive from object_t and their respective pure virtual interface type, and all published (pluggable) interfaces derive from interface_t.

Then you can load a plugin, instantiate the interface using your factory and delete thusly -

delete interface->object()

And as object_t has a virtual destructor the correct destructor will be called.

Theres no problem with where you delete the object under linux, its fine under window's if all plugins/executables are linked against the same dynamic (dll) CRT.

Upvotes: 1

adf88
adf88

Reputation: 4442

You don't need destructor in the interface at all. Proper destructor must be seen only by delete caller. Outside the DLL, use an interface pointer as a handle, not something you can create / destroy. Object construction / destruction should be be inside the DLL. You can use reference counting or any other technique you want, just provide proper exports to access and manipulate objects, you decide how much memory management will be encapsulated.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283614

You shouldn't mix and match calls to new and delete across DLL boundaries.

Instead, I'd recommend the tried-and-true method that COM uses: AddRef and Release. When the reference count hits zero, Release calls delete this; from inside the DLL, ensuring that new and delete are matched.

Upvotes: 4

Related Questions