0
shared_ptr<std::string> shared_ptr1 = std::make_shared<std::string>("Foo");
shared_ptr<std::string> shared_ptr2 = std::make_shared<std::string>("Bar"); 

std::string* normal_ptr = shared_ptr1.get(); 
shared_ptr1 = shared_ptr2; 

Now, will the first string "Foo" be garbage collected after the "shared_ptr1 = shared_ptr2" assignment? According to this , "Foo" isn't garbage collected. But I just want to make sure that what I am encountering isn't an undefined behavior.

Thank you!

9
  • 2
    We don't really know exactly what you're encountering. But I suspect it has to do with the pointer normal_ptr becoming invalid after the assignment shared_ptr1 = shared_ptr2? Commented May 21, 2019 at 17:24
  • 1
    @molbdnilo std::shared_ptr can be seen as C++'s way of supporting reference counting. Reference counting is a valid form of garbage collection. I believe the view point that std::shared_ptr is a form of garbage collection to be fair. Commented May 21, 2019 at 17:25
  • 1
    You are reading in deleted memory. If you run your code with valgrind, you'll see the errors. Commented May 21, 2019 at 17:26
  • 2
    You can’t deduce anything from the code you linked; it has undefined behaviour. Commented May 21, 2019 at 17:35
  • 1
    @FrançoisAndrieux shared_ptr is a shared object, with a lifetime as long as its longest-lived owner. Reference-counting is the (rather obvious) implementation mechanism supporting shared ownership, not the other way around. Commented May 21, 2019 at 17:45

2 Answers 2

4

Now, will the first string "Foo" be garbage collected after the "shared_ptr1 = shared_ptr2" assignment?*

If you mean will the string that was allocated by std::make_shared<std::string>("Foo") be destroyed, then yes.

That means that normal_ptr, after you do shared_ptr1 = shared_ptr2; points to an object that no longer exists.

Sign up to request clarification or add additional context in comments.

Comments

2

I would not use the phrase "garbage collected". After the mentioned assignment, the object will be simply deleted because no shared_ptr points to it. You can test it with the following code:

struct S{
    std::string s;
    S(std::string s){
        this->s = s;
    }
    ~S(){
        std::cout << "~S() with string " << s << std::endl;   
    }
};

int main()
{
    std::shared_ptr<S> shared_ptr1 = std::make_shared<S>("Foo");
    std::shared_ptr<S> shared_ptr2 = std::make_shared<S>("Bar"); 

    S* normal_ptr = shared_ptr1.get(); 
    std::cout << "after get()" << std::endl;
    shared_ptr1 = shared_ptr2;
    std::cout << "end of main" << std::endl;
}

which gives

after get()
~S() with string Foo
end of main
~S() with string Bar

which means that after the assignment, normal_ptr points to non-existing object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.