PDA

View Full Version : Help me to use QtSharedMemory to share the data objects



gtthang
17th February 2006, 06:15
Hi,

I have some data objects need to be shared between 2 processes. I downloaded QtSharedMemory from Trolltech but I don't know how to use it to share my data objects.
Could you show me some ideas to do that?

Thanks very much!

wysota
17th February 2006, 11:24
Didn't you get some example code with the package? Here is one: http://doc.trolltech.com/solutions/4/qtsharedmemory/processcounter.html

gtthang
17th February 2006, 12:22
Thanks for replying!

I think my problem is design.
For instance, I have 2 processes A and B. I defined a class as following:
class Foo
{
...
QString m_strData;
int m_iData;
}
and I want to share a object of this class between 2 processes. Is there any way to put that object into the shared memory? Could you give me some example codes?

wysota
17th February 2006, 12:50
and I want to share a object of this class between 2 processes. Is there any way to put that object into the shared memory? Could you give me some example codes?

It is not advised to put objects into shared memory. Especially ones, which contain pointers (like QString), because the pointer will be invalid in the other process' address space. Instead use plain types or serialize objects you want to share. So you can use ints, arrays, etc. Using QDataStream for serialization and deserialization can be useful for more complex objects. For simple ones, store them directly. And treat the shared memory as an array of bytes and not as a general storage place, where you can put anything.