passing Objects through shared memory

I want to pass c++ object through shared memory and want to access member functions of that object in another class.so i am creating shared memory and mapping using mmap it’s working fine.it’s returning void pointer and then i am assigning that address to obj pointer pointing to class using overloaded new operator. but when i am assigning value to member data of that object i am getting SIGBUS error.if u have any solution please let me know.if u have any other idea passing objects through shared memory please help me.thanks in advance.

An object cannot be created in shared memory. It’s not support by the language. You might get away with it if you VERY careful but it’s asking for trouble.

What you need to do is the class must contain a pointer to the share memory. Then the constructor set that pointer to the shared memory object ( either creating it or mmap to it ).

Then the functions member of the class must reference the information through that pointer.

And don’t know exactly what you mean to “pass” objects to shared memory. You can “map” objects to shared memory with no problems (i did it many times).

I saw SIGBUS error using shmem in situations where the initial size of the mapped memory is one, and then you want to mmap more bytes with no removing the first mapped device.

Try to rm /dev/shmem/your_mem and try again.

Check you are using in the right way shm_open, ftruncate (no ltrunc!) and mmap and check the size of the shmem is the size you want.

Juan Manuel

Sorry i am new to c++ that’s why i am litlle bit confuse.i am attaching my code to this then tell me what is the problem.i am getting SIGBUS error at obj->a.this my client process.

static void *addr = NULL;

class basic
{
public:
int a;
void print();
void * operator new(size_t size);
};

void *basic::operator new(size_t size)
{
cout<<“no of bytes going to assign”<<size<<endl;
return addr;
}

void basic::print()
{
cout<<"value of a is "<<a<<endl;
}

main()
{
int fd;
basic *obj;

fd = shm_open("/shmem", O_RDWR|O_CREAT, 0777);
if(fd == -1)
{
    cout<<"error"<<endl;
}
addr = mmap(NULL, 1000, PROT_READ|PROT_WRITE, NULL, fd, 0);
perror("mmap");

cout<<"address given by mmap"<<addr<<endl;
obj = new basic;
obj->a = 10;
cout<<"address of obj is"<<obj<<endl;
obj->print();

}

Since you appear to be a beginner in C++ I recommend you don’t share object like this. It’s very dangerous. If someone uses your class as a base class it will break. If the class contained a template function it will break. If the class contain an object that itself is inherited from a base class it will break. If there are virtual functions it will break. I probably am missing some…

Your code is missing a ftruncate to set the size of the shared memory. The basic class is also missing an overload for the delete operator.

For cleaner code, shm_open/ftruncate/mmap should be in the new operator of basic. The delete operator should call shm_close/munmap and possible unlink to remove the /dev/shmem/… This will allow getting rid of the global addr variable.

To my knowledge there as been many attempts at making frameworks to allow for seamless sharing of object via shared memory but have all failed. It can’t be generic and flexible enough. But as I said if you know what you are doing you might be able to get away with it ;-)

This is wrong. C++ has explicit language support for exactly this purpose via the “placement new” operator. Google it. The entire class will be constructed at the offset specified by placement new.