Results 1 to 14 of 14

Thread: unable to save QCStrings properly in a buffer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default unable to save QCStrings properly in a buffer

    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.


    Qt Code:
    1. void monitorForm::int()
    2. {
    3. stationNum="1234";
    4. stationLoc="hello";
    5. operatorName="hihi";
    6. operatorTel="12345678";
    7. installationDate="10/10/2000";
    8. maintenanceDate="22/22/2000";
    9. saveInfo(operatorTel,operatorName,stationNum,stationLoc,installationDate,maintenanceDate);
    10. }
    11.  
    12. /*.....*/
    13.  
    14. void monitorForm::saveInfo(QString tempOperTel, QString tempOperName, QString tempStatNum, QString tempStatLoc,
    15. QString tempInstallDate, QString tempMaintenDate)
    16. {
    17. //convert to ASCII
    18. QCString tOT = tempOperTel.local8Bit();
    19. QCString tON = tempOperName.local8Bit();
    20. QCString tSN = tempStatNum.local8Bit();
    21. QCString tSL = tempStatLoc.local8Bit();
    22. QCString tID = tempInstallDate.local8Bit();
    23. QCString tMD = tempMaintenDate.local8Bit();
    24.  
    25. int tOTL = tOT.length();
    26. int tONL = tON.length();
    27. int tSNL = tSN.length();
    28. int tSLL = tSL.length();
    29. int tIDL = tID.length();
    30. int tMDL = tMD.length();
    31.  
    32. const char *savingStrs=tOT+tON+tSN+tSL+tID+tMD;
    33. int savingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
    34.  
    35. printf("savingStrs = %s\n",savingStrs);
    36. printf("%d+%d+%d+%d+%d+%d = %d\n",tOTL , tONL , tSNL , tSLL , tIDL , tMDL , savingLength);
    37.  
    38. //lock the file for writing
    39. memset (&lock, 0, sizeof(lock));
    40. lock.l_type = F_WRLCK;
    41. fcntl (fd, F_SETLKW, &lock);
    42.  
    43. //file_memory is a pointer to the zero offset of the sh.mem. segment
    44. //opcode offset is just an offset in the sh.mem where we write some index values
    45. *((char*)file_memory+wOpcodeOffset) = 9;
    46. struct ShMem *infoVar=(ShMem*)((char*)file_memory);
    47.  
    48. infoVar->oTL = tOTL;
    49. infoVar->oNL = tONL;
    50. infoVar->sNL = tSNL;
    51. infoVar->sLL = tSLL;
    52. infoVar->iDL = tIDL;
    53. infoVar->mDL = tMDL;
    54. printf("hello\n");
    55. memcpy(infoVar->oAndS_str, savingStrs, savingLength);
    56. printf("hello2\n");
    57.  
    58. //unlock the file to allow access
    59. lock.l_type = F_UNLCK;
    60. fcntl (fd, F_SETLKW, &lock);
    61. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jacek; 13th November 2006 at 17:09. Reason: wrapped too long line

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.