Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Understanding warning "Returning data of temporary object"



  • I have written a function to convert QString to char*

        char* qStringToCharPtr(QString string, int* length)
        {
            QByteArray byteArray = string.toLocal8Bit();
            *length = byteArray.length();
            return byteArray.data();
        }
    

    And I receive the warning: "Returning data of temporary QByteArray". I understand that this is because the byteArray object is destroyed after the function call, and I am returning a pointer to the data stored in QByteArray .
    Now change the function to this

        char* qStringToCharPtr(QString string, int* length)
        {
            QByteArray byteArray = string.toLocal8Bit();
            *length = byteArray.length();
            char * ch = byteArray.data();
            return ch;
        }
    

    And with these changes, I don't get the warning. So my question is how come I don't receive the warning with the changes I made? All I did was to create a new pointer that points to the data in byteArray and return that instead?


  • Lifetime Qt Champion

    Hi
    It tries to tell you that byteArray data is very temporary and
    you return a pointer to its internal data.
    In version 2, you hide that fact as
    char * ch = byteArray.data();
    is indeed valid for that line
    and from "return ch; " it cannot know that anymore but the effect is still the same.

    If we look in docs
    https://doc.qt.io/qt-5/qbytearray.html#data
    it says
    "The pointer remains valid as long as the byte array isn't reallocated or destroyed. "

    and QByteArray byteArray is a local variable so as soon as the function ends, it will be dellocated.

    So its better to use .data() in place and not via a function.


  • Lifetime Qt Champion

    Hi
    It tries to tell you that byteArray data is very temporary and
    you return a pointer to its internal data.
    In version 2, you hide that fact as
    char * ch = byteArray.data();
    is indeed valid for that line
    and from "return ch; " it cannot know that anymore but the effect is still the same.

    If we look in docs
    https://doc.qt.io/qt-5/qbytearray.html#data
    it says
    "The pointer remains valid as long as the byte array isn't reallocated or destroyed. "

    and QByteArray byteArray is a local variable so as soon as the function ends, it will be dellocated.

    So its better to use .data() in place and not via a function.



  • That makes sense thanks. Suppose I need to use another object that is very temporary like QByteArray in a function and return some of its internal data, what would be the way to do it? Maybe it is a very niche situation, but still nice to know


  • Lifetime Qt Champion

    @saa_
    Hi
    Well it kinda depends on what we want to return.
    Sometimes we can get away with simply returning a copy.

    Like
    QString SomeFunc()

    then the Qstring does the copying for us.

    if we are talking a char * from a container like qbytearry the best way would be to allocated storage for it
    as docs shows.

    QString tmp = "test";
    QByteArray text = tmp.toLocal8Bit();
    char *data = new char[text.size() + 1];
    strcpy(data, text.data());  
    

    and use that buffer. but that also places the burden on the caller to remember to deallocate the buffer.
    So when this i needed to do then using a smart pointer to hangle the char * buffer can be used.

    However, its not 100% the whole truth

    https://en.cppreference.com/w/cpp/language/lifetime
    (Temporary object lifetime)

    So you might get away with code belov as when part of the calling expressions, the compiler might keep it alive.

      char* qStringToCharPtr(QString string)
        {
            return  string.toLocal8Bit().data();
        }
    

Log in to reply