I’m taking a deeper look into the new C++11 features. One of the most useful
features turned out to be
a smart pointer that, along with
superseded the almost useless
auto_ptr, which is now deprecated.
shared_ptr wraps a pointer to an object, and has an internal counter
taking track of how many copies of this
shared_ptr are currently managing that object.
When the counter eventually reaches
0, the object is deleted.
This can be very convenient, especially when memory management is an issue since the ownership relationships between objects are not clearly evident — and it’s hard to find out when and how an object should be deleted.
Still, there is a subtle issue that one should take care of when using
I bet it is quite obvious for most experienced C++ programmers, but I think everyone can do a mistake,
and this post may actually save you from a big headache, sometime in the future. Read more...