What is the current correct approach to instantiating objects in shared memory so that multiple processes can access the C++ objects?
I don't know the answer to your question. However, I am wondering, are your two processes executing the same executable?
I'm sure you're assumption is correct: the C++ object contains pointers to its virtual table (if it has virtual functions).
My guess is that if you running the same executable, and you're having problems, then the virtual dispatch tables are dynamically allocated and you're screwed unless there is some magic g++ option I don't know about.
However, you could also work around this by using function pointers stored in the class instances (if you are using the same executable in which case I *think* the functions would be loaded at the same spots in the two processes memory space), or, more robustly, indexes into a table of function pointers shared by the two processes.
Here is an example of the second option, which should work even if you have different executables:
typedef void (*VFuncSig) (void);
struct VFuncTable { VFuncSig mMyVirtualFunc1; // two different versions VFuncSig mMyVirtualFunc2; // ...of the same function };
class B {
B (VFuncTable *vt) { mMyVirtualFunc = vt->mMyVirtualFunc1; }
VFuncSig mMyVirtualFunc; void MyVirtualFunc () { mMyVirtualFunc (); }
};
class C : public B { C (VFuncTable *vt) : B(vt) { mMyVirtualFunc = vt->mMyVirtualFunc2; } };
You have to initialize the VFuncTable once in the beginning of the program. You'd probably want to pass in the this pointer, now that I think about it, or make those be C++ method pointers, but I don't know much whether any vtables are instantiated if you use the latter.
Just a thought. Obviously a pain to maintain. Hopefully someone else will respond with an easier way...
Niko