
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