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.