Boost logo

Boost Users :

Subject: Re: [Boost-users] [Interprocess] shared_ptr in shared memory and inheritance
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-02-09 16:00:57


Gaetan Gaumer wrote:
> Hello,
> we would like to store collections of differents objects derived from a
> same base class in a shared
> memory segment, and use polymorphism to manipulate them using
> shared_ptr<base>.
> But as we can't have virtual functions in shared memory we are exploring
> this solution :
> We designed 2 parallels hierarchies of classes
> - A hierarchy of empty objects in process memory with virtual functions
> (polymorphism) (ex : class A)
> - A hierarchy of data objects in shared memory without virtual functions
> (class A_data)
>
> A "process memory" object stores a weak_ptr to the "shared memory" data
> object.
> Use of data are encapsuled in accessors which take care of weak_ptr
> locking and concurrent access.
>
> I don't know if this is a good design (any opinions ?) but it seems to
> work well... until the destruction
> of "shared memory" objects.
> The wrong destructor is used (the base one) instead of the destructor of
> the derived class,
> probably because the destructor is not virtual...
> So I'm currently stuck and I'm looking for some advices.

First of all, if you use fixed_managed_shared_memory, you want to
specify the same base address in ever process, otherwise, you're code
won't work.

You solve your issue, I'm afraid you will need to build a table of
pointers to destructors indexed by a unique number per type (the same
number in all processes), so that when the base class destructor is
called, it makes a lookup in the table and calls the correct
"destructor" function. Not easy, but I think it's the only way. Your
hierarchy couldn't be expanded by users (they need to register their
destructor). It's a limited type of "polymorphism".

Regards,

Ion


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net