Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem using QTextStream as local variable
QtWS25 Last Chance

[SOLVED] Problem using QTextStream as local variable

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 976 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    primem0ver
    wrote on last edited by
    #1

    Here is the code and problem I am experiencing in a method I have. Like any responsible programmer I want to make sure that I do not let a dangling pointer go undeleted...

    @
    public someFunction()
    {
    QByteArray* mResponse = new QByteArray();
    QTextStream outds(mResponse, QIODevice::WriteOnly);

    // do something with stream/array

    delete mResponse; // doesn't cause error directly BUT

    } // error occurs here when leaving the method if I delete mResponse.
    @

    Basically if I delete mResponse, the program crashes upon leaving the method giving an Access Violation. The only reason I can think of for this is that QTextStream attempts to delete the QByteArray as part of its destruction... a pointer that has already been deleted. Is this true?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      No, QTextStream doesn't delete the QByteArray, but QTextStream::flush() will be called when destroys the QTextStream.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to 1+1=2, you don't need to allocate mResponse on the heap

        @
        QByteArray mResponse;
        QTextStream outds(&mResponse, QIODevice::WriteOnly);

        // do something with stream/array
        @

        is the usual pattern in this case

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved