cleaning up in a different library

P

puzzlecracker

It is sort of open-ended question, thus pardon the clarity herein.

Let's say I have library (dll, .so, doesn't matter really), and I pass
it a pointer, to Foo (Foo *). How should that library know to clean up
after, what is the best design of this sort of a thing? I usually go
with two cases:
a) I pass it in a smart pointer
b) as a raw pointer

Let's say that b is mandatory, what should the strategy, or common
design be like?


Thanks
 
A

Alf P. Steinbach

* puzzlecracker:
It is sort of open-ended question, thus pardon the clarity herein.

Let's say I have library (dll, .so, doesn't matter really), and I pass
it a pointer, to Foo (Foo *). How should that library know to clean up
after, what is the best design of this sort of a thing? I usually go
with two cases:
a) I pass it in a smart pointer
b) as a raw pointer

Let's say that b is mandatory, what should the strategy, or common
design be like?

Cleanup responsibility should never implicitly be passed across module
boundaries. Pass a cleanup function explicitly. That function can of
course be a class member if proper care is exercised (e.g. for Windows
dynamic libraries it should not be inline, because you want the executed
code to be in the original allocating library).
 
J

Joe Greer

* puzzlecracker:

Cleanup responsibility should never implicitly be passed across module
boundaries. Pass a cleanup function explicitly. That function can of
course be a class member if proper care is exercised (e.g. for Windows
dynamic libraries it should not be inline, because you want the executed
code to be in the original allocating library).

It turns out that at least under Windows and I believe everywhere, that if
you have virtual destructor, the deallocation always happens in the correct
place. Of course this mandates a vtbl or some such. Otherwise, as Alf
mentioned, you should provide a function to do the cleanup in the proper
module (Note: inline functions won't this).

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top