PDA

View Full Version : problem with saving QCstrings



nass
5th October 2006, 13:04
hello everyone.. i am not sure what is wrong - never liked strings anyway!
my problem is that i am trying to save a QCstring in a shared memory segment and i get a segmentation fault..
here is the case statement that causes the trouble.. i could post the whole procedure but there is no point. similar case statements as the following one exisst in the procedure that save in the same memory segment (but at dif offest) floats, Q_UINT8 and other non string type of variables.


case 4: //the scheduler capacitors are calling
{
struct schedStruct {
Q_UINT8 c1TD;
Q_UINT8 c2TD;
Q_UINT8 c3TD;
QCString c1OnDate;
QCString c2OnDate;
QCString c3OnDate;
QCString c1OffDate;
QCString c2OffDate;
QCString c3OffDate;
};

//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
printf("locked the file\n");

*((char*)file_memory+opcodeOffset) = 4;
struct schedStruct *schedVar=(schedStruct*)((char*)file_memory + 0xCC);
schedVar->c1TD=c1TimerDaily;
schedVar->c2TD=c2TimerDaily;
schedVar->c3TD=c3TimerDaily;
printf("done with dailys\n");
schedVar->c1OnDate=(c1TimerOnDate.local8Bit());
printf("done with qcstring1On\n");
schedVar->c2OnDate=c2TimerOnDate.local8Bit();
schedVar->c3OnDate=c3TimerOnDate.local8Bit();
schedVar->c1OffDate=c1TimerOffDate.local8Bit();
schedVar->c2OffDate=c2TimerOffDate.local8Bit();
schedVar->c3OffDate=c3TimerOffDate.local8Bit();
printf("saved them");

//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
printf("unlocked\n");

file_memory is void* to the beginning of the sh.mem.segment.
cXTimerDaily are unsigned chars
and
cXTimerOnDate, cXTimerOffDate are normal 16bit QStrings containing a date in the form:
'08:00 - 01/01/2006' - ie just ascii chars therefore i should have no problem doing conversion to 8bit strings (as QCStrings are)..
anyway,
i run the gdb and i can get up to the line ' schedVar->c1OnDate=(c1TimerOnDate.local8Bit());'
(between the 2 printf() lines).
then i use the next command. here is as far as i get :


877 printf("done with dailys\n");
(gdb) next
done with dailys
249 { return (QCString&)assign( s ); }
(gdb) next
79 { return (QMemArray<type>&)QGArray::assign(a); }
(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0xb7df1098 in QGArray::assign () from /usr/lib/qt/lib/libqt-mt.so.3
(gdb)

any ideas how to tackle this?
nass

wysota
5th October 2006, 13:10
Don't store any data containing pointers in a shared segment if you intend to use it from another process - all pointers will become invalid. Only store serialised data. If you need to transfer a complex structure - convert it into a character string (serialise).

nass
5th October 2006, 13:23
Don't store any data containing pointers in a shared segment if you intend to use it from another process - all pointers will become invalid. Only store serialised data. If you need to transfer a complex structure - convert it into a character string (serialise).

could you elaborate on this abit more?
what and how do you mean to serialize the data?
how can i do that? for the QCStrings case.. do i also need to serialize the three Q_UINT8 variables? or they are fine since they are not pointers to variables..?
thank you for your help
nass

wysota
5th October 2006, 15:12
could you elaborate on this abit more?
what and how do you mean to serialize the data?
how can i do that?
http://en.wikipedia.org/wiki/Serialization

do i also need to serialize the three Q_UINT8 variables? or they are fine since they are not pointers to variables..?
They are fine.