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. Understanding warning "Returning data of temporary object"
Forum Updated to NodeBB v4.3 + New Features

Understanding warning "Returning data of temporary object"

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.0k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    saa_
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      3
      • S Offline
        S Offline
        saa_
        wrote on last edited by
        #3

        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

        mrjjM 1 Reply Last reply
        0
        • S saa_

          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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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();
              }
          
          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