Virtual Functions And Shared Memory

Object-oriented programming and SharedMemory generally don't work well together.

Many object-oriented languages such as CeePlusPlus implement inheritance using a virtual function table (vtable for short). For each object class in your program, the compiler creates a vtable pointing to the location of each function implemented by that class. When you create an instance of an object, that instance contains the object's data members, plus a pointer to the vtable; when you invoke a virtual (inheritable) function on that object, the compiler inserts code to find the function in the object's vtable and jump to it.

This approach allows different subclasses to have different implementations of the same function: given a superclass A which defines a virtual function foo(), subclass B's vtable will map foo to B::foo(), while subclass C's vtable will map foo to C::foo().

This works fine, as long as there's only one process involved. When you try to pass objects between two processes using SharedMemory, though, you get problems:

There is no way, in C++, to control where the vtable goes or even to gain access to it from program code, so it's not an option to put that into shared memory. Even if you could access the vtable, it's implemented differently on each platform and compiler, so you don't know what information to copy to shared memory or how to access it.


Some ways to get around this problem are:

Has anybody come up with other ways of dealing with this? I HaveThisProblem and have no choice but to use shared memory for interprocess communication (old protocols and such). Any contributions would be greatly appreciated. --RickSamuels

I've always solved this problem with serialization/deserialization. There's no point copying the vtable to shared memory. Just copy the data. Alternatively, avoid copying objects between processes in the first place by using some equivalent of remote procedure calls. It's often cheaper to copy arguments than objects. -- EricHodges


It seems to me that it's likely to just work.

Let's assume

  1. All processors accessing this object are identical (homogeneous).
  2. Each processor accessing this object is running an identical application.
  3. The OS loading the application has placed that application in precisely the same (virtual) address in every processor. It doesn't matter if there is only one copy of the application in the shared memory (the same physical address), or if there is one copy in each processor's local memory.

Practically all shared-memory computers already do all 3 of these things -- right ?

Then what happens is

(members of pointer type are still a problem).

That only works when the communicating processes are running the same program and can get completely blown away by Address Space Layout Randomization (ASLR). It's not recommended on modern operating systems.


Interested:


CategoryCpp


EditText of this page (last edited August 8, 2011) or FindPage with title or text search