Results 1 to 9 of 9

Thread: How to avoid implicit sharing of QString ?

  1. #1
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question How to avoid implicit sharing of QString ?

    Hi,

    I have a strange crash in a small app of mine, and I could trace it back to something I believe is related to implicit sharing of a QString.

    This is sort of what happens :
    - i receive a text message over TCP/IP
    - this message is parsed using QJSON
    - a value of a certain field is taken from the QJSON output as a QString
    - this value is shown in a QLineEdit using setText()

    Now, when the same value is written twice to the QLineEdit, a crash occurs.

    This is my stack trace :
    0 DbgBreakPoint ntdll 0x7c90120e
    1 RtlpNtMakeTemporaryKey ntdll 0x7c96f26e
    2 RtlValidateHeap ntdll 0x7c962fe0
    3 HeapValidate kernel32 0x7c85f9a7
    4 CrtIsValidHeapPointer MSVCR80D 0x102115ba
    5 free_dbg MSVCR80D 0x102107a6
    6 free_dbg MSVCR80D 0x1021065e
    7 free MSVCR80D 0x102105de
    8 qFree qmalloc.cpp 60 0x4ab8bd
    9 QString::free qstring.cpp 1108 0x5135bf
    10 QString::~QString qstring.h 869 0x47bff3
    11 QLineControl::internalSetText qlinecontrol.cpp 637 0x655d3349
    12 QLineControl::setText qlinecontrol_p.h 577 0x65560b07
    13 QLineEdit::setText qlineedit.cpp 384 0x655caa06
    So the crash is really inside Qt's internal functions. The crash does NOT occur if I first copy the value to another string and modify it (e.g. when I do toUpper()). But when I just copy the string, or when the original is already uppercase, then the crash occurs. This shows to me that the problem only occurs when the value written to the QLineEdit is an implicit copy of something else.


    The quick-and-dirty solution so that I can move on with the program is to make a copy of the string that is not implicitely shared. But is there a way to do that ?

    Regards,
    Marc

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid implicit sharing of QString ?

    Implicit sharing doesn't cause crashes. It's likely you are using threads or something else that causes the crash and it only manifests itself inside QString code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to avoid implicit sharing of QString ?

    Hi Wysota,

    I'm not using threads.

    My packet handling routine is called from within the ReadyRead() signal of the client connection, and this one is created by the TCP server ( I get it using nextPendingConnection() ). The TCP server is created with my main dialog as its parent, so all its events and those of the connections it creates should be processed by the main event loop I think.

    Right now I'm just looking for a workaround, so I can test the rest of my application, and deal with the real problem later on.

    Regards,
    Marc

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to avoid implicit sharing of QString ?

    - a value of a certain field is taken from the QJSON output as a QString
    - this value is shown in a QLineEdit using setText()
    Could you post a some code related to above steps, I guess the problem might be there, also how do you set the text, is it using a signal/slot or a direct call?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid implicit sharing of QString ?

    Aren't you passing pointers to strings anywhere?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to avoid implicit sharing of QString ?

    Hi,

    Unfortunately everything is part of a rather complicated project, so it is not easy to just give a piece of code. I would have to further isolate the problem (which I'll do shortly).

    The TCP packet is first received and passed on as a QByteArray, and everything is passed on 'by reference' (which is internally a pointer indeed, but I don't do any 'pointer magic' or casting). The QByteArray is read by QJson and the value of a certain field is returned as QString. Then this QString is passed on by value to my function that sets the text in the QLineEdit.

    Anyway, this code has been working before, so I'm sure it's just something silly somewhere, but I need to focus on other parts of the program right now. So I'd just like a quick workaround at this point.

    Regards,
    Marc

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid implicit sharing of QString ?

    As I said, the fact that the fault manifests itself in here doesn't mean the fault is actually here. You might be chasing ghosts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to avoid implicit sharing of QString ? (solved)

    Hi,

    I rebuilt everything from source (including the QJSon library) and everything works fine now. It had probably to do with the fact that the QJson library was build a long time ago and maybe with a different Qt version as the one I'm using now.

    Santosh, Wysota, thanks for the replies anyway.

    Still, I would have liked to know how you can make a 'deep copy' of a string or other object that uses implicit sharing behind the scenes.

    Regards,
    Marc

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid implicit sharing of QString ? (solved)

    You can call detach() method on the string (it's an undocumented method but it's there).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 9th November 2011, 23:20
  2. Classes with Implicit Sharing
    By weaver4 in forum Newbie
    Replies: 5
    Last Post: 15th January 2011, 18:57
  3. Threads and Implicit Sharing
    By stillwaiting in forum Newbie
    Replies: 5
    Last Post: 30th November 2010, 16:46
  4. Question about implicit sharing
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2009, 17:03
  5. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 19:09

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.