S
sip.address
I'm using reference counted smart pointers in a small project and it's
a breeze, but I'm running into a problem which I don't know how to
approach.
Consider something like this:
class A {
public:
A() {}
void setObserver(counted_ptr<B> observer) { _observer =
observer; }
private:
counted_ptr<B> _observer;
};
class B {
public:
B() {
_a = counted_ptr<A>(new A);
_a->setObserver(counted_ptr<B>(this));
}
private:
A's destructor is called (while B is being destroyed), it will destroy
_observer, which having a ref count of 1, will delete the pointed
object (B), which is not a good thing.
The easiest alternative I thought for this case is using a reference
instead, but I'd like to know if there's some way to fix this problem
(having an extra reference to avoid deletion somehow, etc)
Thanks!
a breeze, but I'm running into a problem which I don't know how to
approach.
Consider something like this:
class A {
public:
A() {}
void setObserver(counted_ptr<B> observer) { _observer =
observer; }
private:
counted_ptr<B> _observer;
};
class B {
public:
B() {
_a = counted_ptr<A>(new A);
_a->setObserver(counted_ptr<B>(this));
}
private:
already existing raw pointer. In this case, the problem is that whenFrom what I can see, the problem is creating a smart pointer with an
A's destructor is called (while B is being destroyed), it will destroy
_observer, which having a ref count of 1, will delete the pointed
object (B), which is not a good thing.
The easiest alternative I thought for this case is using a reference
instead, but I'd like to know if there's some way to fix this problem
(having an extra reference to avoid deletion somehow, etc)
Thanks!