There are some nuances to
std::auto_ptr that can be problematic if you're not careful. It's even been deprecated in C++11. As a general rule, we just avoid
std::auto_ptr to avoid the problems.
See this article for more details.
Watch out for cyclic references when using reference counting smart pointers.
When using reference counting pointers (
Ptr types), avoid cyclic references of objects. These could lead to memory leaks, which (ironically) defeats the purpose of the smart pointers in the first place.
Pass smart pointers as
const & instead of by value
When passing smart pointers (
Ptr types, Ice
Prx types) as function parameters, pass them as
const & instead of pass-by value. This saves on some needless intermediate copies, since the caller should hold their reference to the object for the duration of the call.
But do not store members or local variables as
const &. This would fail to increment reference counts, so an object may get deleted before its time.
volatile keyword, by its very definition is compiler specific. Its usage could lead to portability problems and is best avoided.
Do Not Allow Exceptions to "Escape" Destructors
An uncaught exception thrown from a destructor when the stack is unwinding due to a prior exception results in
terminate(). If a destructor calls code that may throw, it should surround that code in a