When an object b internally refers to and uses an object a it does not own, the death of a can make b sick. Here is a minimal example to illustrate the point:
#include <iostream>
const int my_int = 5;
class A {
private:
int n_;
public:
int n() const { return n_; }
A(int);
};
A::A(int n__) : n_(n__) {}
class B {
private:
const A *ap_;
public:
int m() const { return 1 + ap_->n(); }
explicit B(const A *);
};
B::B(const A *const ap__) : ap_(ap__) {}
int main()
{
std::cout << "Will put an unnamed A on the heap.\n";
A *const p = new A(my_int);
std::cout << "Have put an unnamed A on the heap.\n";
std::cout << "p->n() == " << p->n() << "\n";
B b(p);
std::cout << "b. m() == " << b. m() << "\n";
std::cout << "Will delete the unnamed A from the heap.\n";
delete p;
std::cout << "Have deleted the unnamed A from the heap.\n";
std::cout << "b. m() == " << b. m() << "\n"; // error
return 0;
}
Of course, one could fix this by letting b keep a copy of a rather than a pointer to it, but suppose that b prefers not to keep a copy (because a occupies a lot of memory or for some other reason). Suppose that b prefers merely to refer to the existing a. When a quietly dies, b never notices. Then, when b tries to use a, unpredictable behavior results.
On my computer, the example’s output happens to be this:
Will put an unnamed A on the heap.
Have put an unnamed A on the heap.
p->n() == 5
b. m() == 6
Will delete the unnamed A from the heap.
Have deleted the unnamed A from the heap.
b. m() == 1
However, on your computer, the result might be a segfault or who knows what.
My example’s trouble seems to lie in that the example indirectly breaks the encapsulation of b, leaving it to the programmer to remember that the continued validity of b depends on the continued existence of a. When the programmer forgets, the program breaks. The harassed programmer thus is required to keep b in mind whenever he works on a even though the type A as such does not care about the type B. As you know, object-oriented programmers prefer not to have to keep such trivia in mind if they can help it.
I meet this problem under more complicated guise now and then while programming. I have met it again today. One feels that, somehow, there should exist an elegant design pattern to maintain the proper encapsulation of b, to transfer from the programmer to the compiler the responsibility of remembering b‘s dependence on a‘s existence — and that the pattern fundamentally should involve something less elaborate than smart pointers and full-blown reference counting. However, maybe I am wrong. Maybe this is exactly what smart pointers their reference counting are for. Either way, I know neither the right pattern to apply against the problem nor the best way to fix the code.
If you do know, would you tell about it?
Here is the most nearly related answer I notice already on Stackoverflow; but, besides using one or two words I do not understand, that answer does not seem to answer this question, anyway.
(My compiler still does not happen to support C++11 very well, but if C++11 brings a feature specifically meant to address my problem, then of course I should be interested to learn of it. Admittedly however, my question mainly concerns OO/scoping fundamentals. The question is even more interested in the underlying design pattern than in this or that new feature of the latest compiler.)
NOTE TO THE READER
Some good answers have graced this question. On Stackoverflow, as you know, the asker has the responsibility to accept the best answer so that (when you read this months or years later) you will not have to search for it.
However, it is the combination of two answers that best answers this question. You should read both:
I can think of two particular solutions:
In the shared ownership solution, the lifetime of
ais determined by a counter of the number of owners, only when there is no owner any longer doesa‘s lifetime come to an end. This is typically implemented usingstd::shared_ptr<A>.In the observer solution, when
ais passed tob,amemorizes thatbholds a reference to it. Then, whenadies, either it notifiesbright away or leave a “token” behind forbto be notified at the next access attempt.The direct notification is usually handled by maintaining a list of the current referrers and calling to each of them at destruction time (so they can erase their reference). It is the typical observer pattern.
The indirect notification is usually handled by having a proxy object to go through to get to
a. Whenadies the proxy is notified (O(1)) and whenbattempts to accessait must pass through the proxy. There are various difficulties in implementing this on your own, a better approach is therefore to use standard facilities:std::shared_ptrthis time combined withstd::weak_ptr.Finally, these solutions are not equivalent.
acannot die whilstblives while an observer scheme allowsato die firstbreact immediately abouta‘s death, but at the same time may make the program more brittle (duringa‘s destructor execution, you should not throw any exception)Choose your own poison 🙂