Results 1 to 11 of 11

Thread: semi-random crashes in QByteArray::data()

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default semi-random crashes in QByteArray::data()

    Alright, i am reading into a QByteArray like this:


    Qt Code:
    1. if(myArray.size() < size)
    2. myArray.resize(size);
    3.  
    4. __memcpy_dgsint(dataArray.data(), start, size);
    To copy to clipboard, switch view to plain text mode 

    then, some seconds after doing that, i access the read data:

    Qt Code:
    1. char* r = myArray.data();
    To copy to clipboard, switch view to plain text mode 


    this works just fine most of the time ("start" is sanitized in the second sample).
    Howver, i get crashes with a certain, very specific input file.
    In such a case, the crash happens inside myArray.data(), inside internal QByteArray methods, inside a memcpy() function.
    See the callstack here:



    And the exception:
    First-chance exception at 0x1026ed6a (msvcr90d.dll) in x.exe: 0xC0000005: Access violation reading location 0x00a99f00.
    Unhandled exception at 0x1026ed6a (msvcr90d.dll) in x.exe: 0xC0000005: Access violation reading location 0x00a99f00.

    The size-values we are talking about here arent huge - ~500 bytes maybe.

    This seems all very wired to me. I collected some data and there are no extraordinary values involved when it crashes. Rather, the same call with the same size value succeeded several dozen times before, and then
    QByteArray decides to wiredly call some realloc functon and crash.

    any ideas why?


    winxpsp3, vs2008 + Qt484
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: semi-random crashes in QByteArray::data()

    Which line of your code triggers the crash?

    If you keep and use "r" later then you are inviting problems. QByteArray can and will move its data block as it needs to grow or shrink.

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: semi-random crashes in QByteArray::data()

    the .data() call triggers the crash:

    char* r = myArray.data();

    (does r change, even if i do not call resize()?)

    Anyways, the crash happens before r is set.

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: semi-random crashes in QByteArray::data()

    Well, I googled __memcpy_dgsint, and can't find anything. What does this function do? Is this custom function, maybe it copies wrong number of bytes?
    Sometimes functions from custom API take size not in bytes, but in some other units (say in 16-bit words).
    It's also unclear how you initializing start.
    The fact that program works most of the time, doesn't really prove anything. You may corrupt memory each time and just get "lucky" that nothing important is ruined.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: semi-random crashes in QByteArray::data()

    the .data() call triggers the crash:
    Uh huh, which one? There are two in your original post.

    Calling data() causes a deep copy of the data block if the QByteArray is currently shared. If the data block is very large this might fail. Since it fails:
    i get crashes with a certain, very specific input file.
    it might help to consider what is different about that file. Is it huge?

    does r change, even if i do not call resize()?
    The value of r won't change unless you change it. The memory address of the QByteArray may move if you do anything non-const to the byte array. If that happens the value of r, which has not changed, becomes an invalid pointer.

    I am with lanz, perhaps you should be using standard memcpy() rather than some compiler internal function (any name starting with two underscores is compiler implementation reserved and should not come from user code).

  6. #6
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: semi-random crashes in QByteArray::data()

    Is it huge?
    in comparison the the other files, yes! But still only a couple of MB.
    In the vast majority of the crashes, the second data() call triggers it, but i've seen the first error out, too (after resizing() to hold a lot of data).

    I have replace the dsgin call with this:

    memcpy(myArray.data(), start, size);
    with the same results.


    I am not knowingly sharing the arrray, all i do is reserving() and memcpy()ing into it.

    Thanks for the help this far, i'll run some more tests.

  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: semi-random crashes in QByteArray::data()

    Do you have worker threads in your app? Does more than one thread have access to the byte array?
    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
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: semi-random crashes in QByteArray::data()

    I'd also looked into loading code as well, maybe something with initialization of "start" variable.

  9. #9
    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: semi-random crashes in QByteArray::data()

    I am not knowingly sharing the arrray, all i do is reserving() and memcpy()ing into it.
    Your code shows a resize() and you say reserve(), I hope you know these are two different functions, and have different behaviour. As data() returns a \0-terminated array, the position of null termination will be different for reserve() and resize() calls.

    Make sure when you update(memcpy) the content of data() it is \0-terminated data()[size+1] = '\0';
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  10. #10
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: semi-random crashes in QByteArray::data()

    There are no threads involved, and the loading code looks fine to me.


    Make sure when you update(memcpy) the content of data() it is \0-terminated data()[size+1] = '\0';
    This may be the reason. Thanks!
    Let me check that...

    And yes, i meant resize().

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: semi-random crashes in QByteArray::data()

    Make sure when you update(memcpy) the content of data() it is \0-terminated data()[size+1] = '\0';
    There is no requirement on memcpy() that either the source or destination be null-terminated. memcpy() copies exactly the number of bytes it is told to copy, and those bytes can contain anything.

Similar Threads

  1. Replies: 1
    Last Post: 8th November 2012, 23:23
  2. Replies: 4
    Last Post: 21st October 2012, 19:04
  3. How to debug random crashes?
    By Gunnar in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2011, 21:25
  4. QSerial problem:receives random data
    By omegaKnot in forum Qt Programming
    Replies: 5
    Last Post: 27th May 2011, 09:32
  5. Replies: 1
    Last Post: 7th April 2010, 16:26

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.