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. Data returned from another class is garbage
Forum Updated to NodeBB v4.3 + New Features

Data returned from another class is garbage

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 362 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I guess this a combination of a C++ and Qt question. More C++, but I'm using Qt as my framework.
    I have an external class which has a bunch of methods. In one method it returns a pointer to a structure i.e. myStrcture *results.

    In another class I'm calling the method from the other class to get results, but after I leave the method the results is garbage.

    Example
    myStructure * results = SecondClass::someMethod(arguments)
    //if I look at results it's garbage

    Second class:
    mystructure *
    SecondClass::someMethod(Arguments)
    {
    //do stuff here
    return &results; //So far here is good (when I leave this method I get garbage)
    }

    I also tried this and doesn't help
    SecondClass *test = new SecondClass;
    myStructure * results = SecondClass->someMethod(arguments)
    But still the same results. Any idea how I can get the correct results back. The SecondClass is not a QObject. Not sure if I need that but this I guess is basic C++ issue.

    Thanks

    jsulmJ 1 Reply Last reply
    0
    • L leinad

      Hi,

      I guess this a combination of a C++ and Qt question. More C++, but I'm using Qt as my framework.
      I have an external class which has a bunch of methods. In one method it returns a pointer to a structure i.e. myStrcture *results.

      In another class I'm calling the method from the other class to get results, but after I leave the method the results is garbage.

      Example
      myStructure * results = SecondClass::someMethod(arguments)
      //if I look at results it's garbage

      Second class:
      mystructure *
      SecondClass::someMethod(Arguments)
      {
      //do stuff here
      return &results; //So far here is good (when I leave this method I get garbage)
      }

      I also tried this and doesn't help
      SecondClass *test = new SecondClass;
      myStructure * results = SecondClass->someMethod(arguments)
      But still the same results. Any idea how I can get the correct results back. The SecondClass is not a QObject. Not sure if I need that but this I guess is basic C++ issue.

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @leinad said in Data returned from another class is garbage:

      return &results;

      Where is "results" declared? Is it a local variable inside someMethod or is it class member?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        The results is declared in the method of the second class.

        jsulmJ 1 Reply Last reply
        0
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #4

          As a local variable in that method. Should it be private so it doesn't go out of scope?

          1 Reply Last reply
          0
          • L leinad

            The results is declared in the method of the second class.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @leinad said in Data returned from another class is garbage:

            The results is declared in the method of the second class.

            No wonder it is garbage then.

            SecondClass::someMethod(Arguments)
            {
                SomeStruct results; // results only exists as long as someMethod is being executed
                                    // as soon as someMethod finishes results is destroyed
                                    // so, you return a pointer to destroyed object
                return &results; //So far here is good (when I leave this method I get garbage)
            }
            

            You have two possibilities:

            1. Allocate results on the heap (using new)
            2. Make results class member

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #6

              Great thanks.

              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