A
Arne Claus
Hello
I've written a usefull little smartpointer, implementing reference
counitng. I'm using this class for quite a while but have new
enocuntered a nasty little problem:
After I've created a reference counted Object like this
CRefPtr<A> ptr(new A());
I want to use something implement like this at a completely differend
place in my code
CRefPtr<A> A::getSelf();
This is a problem, as the "A" Object does not know ptr (as it's never
submitted). Thus the only way to return a CRefPtr<A> in this case would
be
return CRefPtr<A>(this);
which would lead to ANOTHER Reference counted object, meaning if ptr is
destroyed but there's still an object created by getSelf() around I get
an access violation. Example:
CRefPtr<A> ptr(new A()); // new
CRefPtr<A> ptr2 = ptr->getSelf(); // new, NOT copy
// consider ptr is now destroyed somehow
ptr2->getMember(); // boom, object does not exist but ptr2 still has ref=1
The only possible solution I found is creating a "self" pointer in each
object and assigning it each time I create a new CRefPtr. So I created
an Interface like
template <class T> CRefPtrInterface() {
public:
CRefPtr<T> self;
// ...
};
where self is set with each constructor call from CRefPtr. The Function
getSelf() would just return self. Now there's something not "nice" with
this solution:
class A : public CRefPtrInterface<A> { ... };
class B : public A { ... };
The latter would have self as CRefPtr<A> not CRefPtr<B> as it would be usefull.
Now (finally my question: Is it possible to get a CRefPtr<B> self in
class B without overloading it "by hand" in each derived class? Would
something like this work?
class B : public A, public CRefPtrInterface<B> { ... };
Thanks
Arne
I've written a usefull little smartpointer, implementing reference
counitng. I'm using this class for quite a while but have new
enocuntered a nasty little problem:
After I've created a reference counted Object like this
CRefPtr<A> ptr(new A());
I want to use something implement like this at a completely differend
place in my code
CRefPtr<A> A::getSelf();
This is a problem, as the "A" Object does not know ptr (as it's never
submitted). Thus the only way to return a CRefPtr<A> in this case would
be
return CRefPtr<A>(this);
which would lead to ANOTHER Reference counted object, meaning if ptr is
destroyed but there's still an object created by getSelf() around I get
an access violation. Example:
CRefPtr<A> ptr(new A()); // new
CRefPtr<A> ptr2 = ptr->getSelf(); // new, NOT copy
// consider ptr is now destroyed somehow
ptr2->getMember(); // boom, object does not exist but ptr2 still has ref=1
The only possible solution I found is creating a "self" pointer in each
object and assigning it each time I create a new CRefPtr. So I created
an Interface like
template <class T> CRefPtrInterface() {
public:
CRefPtr<T> self;
// ...
};
where self is set with each constructor call from CRefPtr. The Function
getSelf() would just return self. Now there's something not "nice" with
this solution:
class A : public CRefPtrInterface<A> { ... };
class B : public A { ... };
The latter would have self as CRefPtr<A> not CRefPtr<B> as it would be usefull.
Now (finally my question: Is it possible to get a CRefPtr<B> self in
class B without overloading it "by hand" in each derived class? Would
something like this work?
class B : public A, public CRefPtrInterface<B> { ... };
Thanks
Arne