Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid memory leak with shared_ptr?

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?

like image 809
Alexey Malistov Avatar asked Dec 01 '09 15:12

Alexey Malistov


People also ask

Should I use Unique_ptr or shared_ptr?

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.

Can smart pointers leak memory?

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.

Do I need to delete a shared_ptr?

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.

How smart pointer avoids the problem of memory leak?

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.

How do I avoid memory leaks with shared_ptr?

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.

How do I avoid memory leakage when using smart pointers?

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.

What causes a memory leak when using owning pointers?

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.

How to avoid memory leaks in C++?

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...


1 Answers

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, if main() holds a shared_ptr to A, which directly or indirectly holds a shared_ptr back to A, A's use count will be 2. Destruction of the original shared_ptr will leave A dangling with a use count of 1. Use weak_ptr to "break cycles."

Thanks, Glen, for the link.

like image 70
James McNellis Avatar answered Sep 28 '22 06:09

James McNellis