general questions
Now I’ve been reading quite a bit about smart pointers, and shared pointers seem like “perfect” in many cases. However I also read about cyclical reference or something like that? Where shared_ptr can’t be used? I’m having a difficult time undestanding this, can someone give a trivial example showing this?
Also I’m really wondering, what do weak_ptr’s provide that normal pointers don’t? – As they don’t increase the reference count they give no guarantee that the memory they point at is still valid?
my personal project:
In a project I’ve 2 “global” containers (both containers are soon to be moved inside a class), both are filled with “objects”. However both should “point” to the same object. An object can not exist outside those containers, and it should not be possible that one container does contain it, while the other doesn’t.
Currently I simply use normal pointers for this, and have a createObject& destroyObject method to manage the memory.
Is this good design? Should I use smart pointers?
To answer your various questions:
Cyclical references are when 2 different objects each have a shared_ptr to the other object.
For example:
This can result in neither of the objects being free’d, as Foo will always keep the reference count to bar above 1, and foo won’t be free’d because bar will always keep it’s reference count above 1.
This is a situation that can be overcome with weak_ptr’s as they do not increment the reference count. As you point out, this will not stop the ptr from being free’d, but does allow you to check that the object still exists before using it, which you could not do with a standard pointer.
As for the example you provided, you should almost always use smart pointers rather than raw pointers, as they allow objects to be free’d automatically when they go out of scope, rather than you having to ensure it is done yourself, which can be error prone. This is especially true in the case where you have exceptions, which could easily skip over any releasing code that you’ve written.
For example, this code could cause problems:
If foo.doSomething was to except, then deleteObject would never be called and foo would not be free’d.
However, this would be safe:
The shared_ptr will automatically be released at the end of the code block, regardless of whether an exception has occurred.
There’s a fairly good discussion of pointers and smart pointers here: Pointers, smart pointers or shared pointers?