1

I think the concept of a pointer that cleans up after it self like std::shared_ptr is cool, but I was wondering if there is a better third party smart pointer out there.

The problem with the shared_ptr is that of recursive references. This occurs when you have something like this:

class A{ public: std::shared_ptr<A> other; A() { } }; //Later std::shared_ptr<A> a = std::make_shared<A>(); std::shared_ptr<A> b = std::make_shared<A>(); a->other = b; //Memory leak b->other = a; //Memory leak 

Is there a smarter smart pointer out there that can sense when I should have used a weak pointer and doesn't leak (or at least gives a warning)? (After a brief google search I can not find anything)

14
  • 1
    Can I ask, why are you doing this? Or is this just a basic example in lieu of something more complicated? With this example you're misusing smart pointers. You wouldn't put delete this in a destructor. Commented Apr 27, 2016 at 23:48
  • Your class A does not compile. Commented Apr 27, 2016 at 23:48
  • A() : self(*this) { } Huh? What is this supposed to do? What are you trying to achieve? Commented Apr 27, 2016 at 23:50
  • It is only an example (and apparently not a compilable one).....Not trying to actually use this code Commented Apr 27, 2016 at 23:51
  • Okay, but it's hard to understand what the actual problem is, when you demonstrate your problem with an example, saying "This occurs ...", when in fact, nothing occurs, because the example doesn't even compile. Commented Apr 27, 2016 at 23:53

2 Answers 2

3

A weak pointer is exactly what should be used here.

The fact that std::shared_ptr implements basic reference counting, and nothing, more isn't something that was discovered recently. C++ is not a managed language, and has no facilities for automatic garbage collection. As such, circular references via std::shared_ptr are problematic. The solution is the weak pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. The problem with reference counting as a GC mechanism is that it can't clean up (by itself) cycles. You've got to do it manually. No smart pointer could detect it for you either at the point of creating the cycle because it wouldn't really know which link was the problematic one. You could imagine a smart pointer implementation that recorded sufficient information during the program so that at the end of a run (or at checkpoint) it could report on leaks (cycles of objects not pointed to externally). But I'm not aware of one (and it would necessarily be "debug only").
It is the purpose of the weak pointer, "std::weak_ptr is used to break circular references of std::shared_ptr. "
0

The solution is not this:

class A{ std::weak_ptr<A> self; public: A() : self(*this) { } }; 

the solution is this:

class A : std::enable_shared_from_this<A> { void use_me() { // say i need to pass a shared_ptr to myself to some other function... foo(this->shared_from_this()); } }; auto var = std::make_shared<A>(); 

6 Comments

That really wasn't my question (I am just trying to see about smarter smart pointers, the example was inconsequential)
when you say "smarter smart pointers" what do you mean? What exactly do you want it to do?
@DarthRubik - then maybe you should think of a different example ... the current example has the drawback (besides the fact it doesn't compile, apparently) that there exists an "answer" which, plausibly, you didn't know. Thus, it is confusing.
I mean one that senses when I should have used a weak pointer
@davidbak I will post an alternative example
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.