I'm on Ubuntu 14.04 using GCC 4.8.4 and I have code similar to the following:
std::shared_ptr<MyClass> my_shared_object = set elsewhere...
MyFunction(*my_shared_object);
Where MyFunction
's signature looks like this:
void MyFunction(const MyClass& my_object)
The full code can be found here
However, I am finding that my_object actually goes out of scope within the context of MyFunction
. My thought was that the my_shared_object
will only release its contents once it goes out of scope, meaning after the MyFunction
has returned. I am not sure if I am either misunderstanding std::shared_ptr
or if maybe this is a GCC bug.
I guess the question boils down to: when I dereference a std::shared_ptr, does that guarantee that the std::shared_ptr
will persist as long as the dereference is being used?