1

QT C++ Can I use Custom mime-type for copy and paste on multiple application?

After I asked that question, I tried to make a application using QSharedMemory.

But When I try to copy and paste between A and A'

Can not read memory occured. (In single Application, It works perfectly)

Below are my result and code.

In single application : works great

In multiple application : cannot read memory

My custom class

class CustomVector{
public: vector<CustomData*> vecs;
};

Copy function

sharedMemory.setKey("TestKey");  // setKey to identify
if (sharedMemory.isAttached()) {
 if (!sharedMemory.detach()) {
  QMessageBox msg;
  msg.setText(tr("Unable to detach from shared memory."));
  msg.exec();
   }
}

    CustomVector* from = new CustomVector(); // 

// do some work for adding CustomData* data to from using pushback (omit)
int size = sizeof(*from);

if (!sharedMemory.create(size)) { // create sharedmemory by size of from
            cout << "Unable to create shared memory segment: " <<     sharedMemory.errorString().toStdString() << endl;
            sharedMemory.detach();
            sharedMemory.create(size);
            cout << "detached" << endl;
        }

sharedMemory.lock();
CustomVector* to = (CustomVector*)sharedMemory.data();
memcpy(to, from, sizeof(*from));
sharedMemory.unlock();

Paste function

sharedMemory.setKey("TestKey"); 
sharedMemory.attach();

CustomVector* to = new CustomVector();

sharedMemory.lock();
CustomVector* from = (CustomVector*)sharedMemory.data();
memcpy(to,from, sizeof(*from));
sharedMemory.unlock();

sharedMemory.detach();
// and do some work using pasted CustomVector* to (omit)

How can I copy and paste on multiple application using QSharedMemory and Custom class that includes vector ?

Community
  • 1
  • 1
  • Try making a shared memory test program (simple program from scratch, I think the docs/SDK even has an example). Can you make shared memory to work at all between processes? – hyde Nov 03 '16 at 06:15
  • I made a shared memory application work at all between processes not include vector but just a Customdata*. 'Customclass with vector' is not work on multiple application now. – HyunWoo Park Nov 03 '16 at 06:59
  • 1
    You appear to be putting a vector of pointers to your custom data into the shared memory. Note that your shared memory would contain the vector of pointers but not the actual data pointed to by these pointers. Can you try to do the same thing by using a vector of `CustomData` objects, not pointers to `CustomData`? – Dmitry Nov 03 '16 at 07:05
  • Yeah, you can't put any pointers to the shared memory, because they would point to bad places in the another application. So I think you can't use any of the Qt implicitly shared data classes, for example, because they use pointers internally. – hyde Nov 03 '16 at 07:15
  • Consider using QDataStream to serialize the data and the store that as just byte buffer in the shared memory. Or create POD structs without pointers to hold the data in the shared memory... Or maybe there's a better way I'm not aware of. – hyde Nov 03 '16 at 07:17
  • @Dmitry I already try CustomData objects. In that case, same thing happened. I thought this happens because vector is container that using pointer. And CustomData is kind of parent class that has many child. so when I use just CustomData object (not CustomData*), some information of child object is gone in single application copy and paste. – HyunWoo Park Nov 03 '16 at 07:18
  • @hyde Using QDataStream is one of the last thing that I can use. But There are a lot of thing that need to be parsing. So I want to use shared memory to reduce work load. – HyunWoo Park Nov 03 '16 at 07:20
  • As @hyde has mentioned, you can't use shared memory in that simple fashion as you are trying. You *must* implement the serialization/deserialization for your classes and hold the serialized data in the shared memory, it must be just a sequence of bytes, not something containing pointers pointing somewhere into the non-shared memory, otherwise it just won't work. – Dmitry Nov 03 '16 at 07:22
  • @HyunWooPark Either you pack your data into a chunk of bytes by serializing it (which is what `QDataStream` does, serializing data using it's own binary format, much more efficient that XML/JSON serialization), or you pack your data into pointerless POD structs (POD means Plain Old Data, ie. basically C-compatible structs). – hyde Nov 03 '16 at 07:52

0 Answers0