Question

I've got a little bug that is blowing my mind. Perhaps it is simple, but I'm totally lost.

I have a basic POD struct:

    struct Data{
    bool isInvalid=false;
    vec3 *vector; //vec3 is another struct with x,y,z components
    Node*node;
    bool isFresh;
    unsigned int *form;
    };

I have a function:

  Data getData(){
    Data forReturn;
    //...populates the forReturn struct
    cout<<forReturn.vector->x; //logs correctly a value 
    return forReturn;
   }

The cout log correctly shows that my return Data has been populated. But when I call this function from another function, a different story presents:

  Data newData=getData(); //logs as above
  cout<<newData.vector->x; //is empty!!

What is going on here?! My log output shows these two lines side by side since they occurring right after the other, but what is going on? This is not multithreaded, so variables and pointers should not be changing between these two lines!

Was it helpful?

Solution 2

I can't tell for sure unless I see the real code you have, but my bet is that your program has undefined behavior, because you are dereferencing a dangling pointer. I believe that in your getData() function you are letting forReturn.vector point to a local object with automatic storage duration, which is destroyed when returning from getData(), like so:

Data getData(){
    Data forReturn;
    vec3 myVector;
    // ...
    forReturn.vector = &myVector;
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // ...
    cout<<forReturn.vector->x; // logs correctly a value 
    return forReturn;
}

In the example above I am returning the Data object forReturn by value, meaning the implicitly declared move constructor (in C++11) or copy constructor (in C++03) will be invoked.

Since these implicitly-generated special member functions perform a memberwise move or copy of the data structure's members, the vector pointer is copied, meaning that what I am returning is an object of type Data whose vector pointer points to an object that has gone out of scope already.

This is a time bomb. As soon as that pointer gets dereferenced, "Boom". Notice, that the "Boom" can actually be a very silent explosion - Undefined Behavior means that anything could happen, including nothing.

OTHER TIPS

If written that way, the local Data (forReturn) is copied during the return-process. So its vital that your copy-constructur of the Data-class is implemented the right way (copies all members the right way)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top