Consider the following code.
using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << "~A" << std::endl; } shared_ptr<B> b; }; struct B { ~B() { std::cout << "~B" << std::endl; } shared_ptr<A> a; }; int main() { shared_ptr<A> a (new A); shared_ptr<B> b (new B); a->b = b; b->a = a; return 0; }
There is no output. No desctructor is called. Memory leak. I have always believed that the smart pointer helps avoid memory leaks.
What should I do if I need cross-references in the classes?
Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.
Even when using smart pointers, is it still possible to have memory leak ? Yes, if you are not careful to avoid creating a cycle in your references.
If you've allocated a shared_ptr dynamically then you're certainly allowed to delete it whenever you want. But if you're asking whether you're allowed to delete whatever object is being managed by the shared_ptr , then the answer is ... it depends.
int * ptr = new int [ 500 ]; // do stuff using the array... The new operator attempts to allocate sufficient memory for the data, and if successful it returns the address of the allocated memory.
By using std::shared_ptr you will avoid most memory leaks There are two types of memory leaks with shared_ptr: Define a deleter which does not delete the object. With deleters you can put FILE objects into shared_ptr, but you can also have a memory leak. Cycles defeat the reference counting of std::shared_ptr.
The only way to avoid memory leaks is to program correctly, there is no method to do such besides that. And smart pointers normally generate something else called memory fragmentation which sometimes its even worst than leakage as its not yet being able to repair. , Developer of the Avalon PL for quantum computers. Just be careful with shared_ptr.
The most common error that can cause a memory leak when using owning pointers, especially shared_ptr, is the deliberate or inadvertent creation of cycles in a graph or linked list.
1 Instead of managing memory manually, try to use smart pointers where applicable. 2 use std::string instead of char *. ... 3 Never use a raw pointer unless it’s to interface with an older lib. 4 The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible – ideally NONE. ... More items...
If you have circular references like this, one object should hold a weak_ptr
to the other, not a shared_ptr
.
From the shared_ptr
introduction:
Because the implementation uses reference counting, cycles of
shared_ptr
instances will not be reclaimed. For example, ifmain()
holds ashared_ptr
toA
, which directly or indirectly holds ashared_ptr
back toA
,A
's use count will be 2. Destruction of the originalshared_ptr
will leaveA
dangling with a use count of 1. Useweak_ptr
to "break cycles."
Thanks, Glen, for the link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With