Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, November 2006
unable to save QCStrings properly in a buffer


Message 1 in thread

hello everyone,
i am having some bit of trouble while trying to save on a shared
memory segment.. just a  bufer really for all interest here. i get a
segmentation fault when i try to save some QCStrings concatenated
together using memcpy.. please have a look.


[CODE]void monitorForm::int()
{
	stationNum="1234";
	stationLoc="hello";
	operatorName="hihi";
	operatorTel="12345678";
	installationDate="10/10/2000";
	maintenanceDate="22/22/2000";
    saveInfo(operatorTel,operatorName,stationNum,stationLoc,installationDate,maintenanceDate);
}

/*.....*/

void monitorForm::saveInfo(QString tempOperTel, QString tempOperName,
QString tempStatNum, QString tempStatLoc, QString tempInstallDate,
QString tempMaintenDate)
{
    //convert to ASCII
    QCString tOT = tempOperTel.local8Bit();
    QCString tON = tempOperName.local8Bit();
    QCString tSN = tempStatNum.local8Bit();
    QCString tSL = tempStatLoc.local8Bit();
    QCString tID = tempInstallDate.local8Bit();
    QCString tMD = tempMaintenDate.local8Bit();

    int tOTL = tOT.length();
    int tONL = tON.length();
    int tSNL = tSN.length();
    int tSLL = tSL.length();
    int tIDL = tID.length();
    int tMDL = tMD.length();

    const char *savingStrs=tOT+tON+tSN+tSL+tID+tMD;
    int savingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;

    printf("savingStrs = %s\n",savingStrs);
    printf("%d+%d+%d+%d+%d+%d = %d\n",tOTL , tONL , tSNL , tSLL , tIDL
, tMDL , savingLength);

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

    //file_memory is a pointer to the zero offset of the sh.mem. segment
    //opcode offset is just an offset in the sh.mem where we write
some index values
    *((char*)file_memory+wOpcodeOffset) = 9;	
    struct ShMem *infoVar=(ShMem*)((char*)file_memory);

    infoVar->oTL = tOTL;
    infoVar->oNL = tONL;
    infoVar->sNL = tSNL;
    infoVar->sLL = tSLL;
    infoVar->iDL = tIDL;
    infoVar->mDL = tMDL;
    printf("hello\n");
    memcpy(infoVar->oAndS_str, savingStrs, savingLength);
    printf("hello2\n");

    //unlock the file to allow access
    lock.l_type = F_UNLCK;
    fcntl (fd, F_SETLKW, &lock);
}[/CODE]

the code crashes on memcpy(...). that is between printf("hello\n");
and printf("hello2\n");

i have noticed something awkard. when i printf savingStrs i get
exactly the following output:
savingStrs =5678hihi1234hello10/10/200022/22/2000

but the savingLength variable returns 41 which is correct and expected.

(notice  that the debugging output doesn't have a space " " after the
equals sign "=". but especially notice that 4 first bytes of the
variable tempOperTel has been trunsated) any ideas?
thank you for your help
nass

--
 [ signature omitted ]