PDA

View Full Version : problem with qsharedmemory



daemonna
16th September 2010, 12:33
i got following code and it looks like due to this code application crash (but still not sure, coz app is multithreaded)...

void simplex_user::user_to_mem()
{
if(!usershm.isAttached())
usershm.attach(QSharedMemory::ReadWrite);
QByteArray usercache;
QDataStream ucd(&usercache, QIODevice::ReadWrite);
usercache.clear();
usercache.resize(44+1);
ucd.device()->seek(0);

ucd << (quint8)this->status << (quint8)this->speed << (quint8)this->playernum // 0 1 2
<< (quint8)this->lookat << (quint8)this->key1 << (quint8)this->key2 // 3 4 5
<< (quint8)this->gotpowerup1 << (quint8)this->gotpowerup2 // 6 7
<< (qint8)this->yoffset << (qint8)this->xoffset << (quint16)this->pos // 8 9 10-11
<< (quint64)this->score << (quint64)this->adminkey << (quint64)this->playerid // 12-19 20-27 28-35
<< (quint64)this->achievements << (quint8)this->flags; // 36-43 44
usershm.lock();
char *to = (char*)usershm.data();
const char *from = this->data.data();
memcpy(to, from, qMin(usershm.size(), usershm.size()));
usershm.unlock();


}

void simplex_user::mem_to_user()
{
bool ok;
QByteArray usercache;
usercache.resize(44+1);

if(!usershm.isAttached())
usershm.attach(QSharedMemory::ReadWrite);
usershm.lock();
usercache = QByteArray((char*)usershm.constData(), usershm.size()+1);
this->data=usercache;
usershm.unlock();
this->status=usercache.mid(0,1).toHex().toUInt(&ok, 16);
this->speed=usercache.mid(1,1).toHex().toUInt(&ok, 16);
this->playernum=usercache.mid(2,1).toHex().toUInt(&ok, 16);
this->lookat=usercache.mid(3,1).toHex().toUInt(&ok, 16);
this->key1=usercache.mid(4,1).toHex().toUInt(&ok, 16);
this->key2=usercache.mid(5,1).toHex().toUInt(&ok, 16);
this->gotpowerup1=usercache.mid(6,1).toHex().toUInt(&ok, 16);
this->gotpowerup2=usercache.mid(7,1).toHex().toUInt(&ok, 16);
this->yoffset=usercache.mid(8,1).toHex().toUInt(&ok, 16);
this->xoffset=usercache.mid(9,1).toHex().toUInt(&ok, 16);
this->pos=usercache.mid(10,2).toHex().toUShort(&ok, 16);
this->score=usercache.mid(12,8).toHex().toULongLong(&ok, 16);
this->adminkey=usercache.mid(20,8).toHex().toULongLong(&ok, 16);
this->playerid=usercache.mid(28,8).toHex().toULongLong(&ok, 16);
this->achievements=usercache.mid(36,8).toHex().toULongLo ng(&ok, 16);
this->flags=usercache.mid(44,8).toHex().toULongLong(&ok, 16);
}



is anything wrong with this code?

daemonna
16th September 2010, 12:54
usershm.lock();
char *to = (char*)usershm.data();
const char *from = this->data.data();
memcpy(to, from, qMin(usershm.size(), usershm.size()));
usershm.unlock();

fixed... usercache replaced this->data.data();... but app still crash...