about kde4's smart pointer

Cyrille Berger cberger at cberger.net
Wed Oct 4 08:47:11 BST 2006


> After looking at the mailing list archive, I now remember the reason
> behind the see for this explicit constructor. It *solve* a possible
> nasty problem with dandling pointers, really hard to debug, if you
> don't declare this constructor explicit (it helps at compile time and
> at bug hunting time). See
> http://lists.kde.org/?l=kde-core-devel&m=113723972022888&w=2
Actually it's easy to debug : NEVER NEVER mix a smart pointer in a normal 
pointer. Well of course with the explicit constructor the problem is solved. 
But you can still do this :
class A : KShared { }
class B { A* a; }
typedef KSharedPtr<A> ASP;

B b;

void foo(B& b_)
{
	ASP a = ASP(new A);
	b_->a = a.data();
}

foo(b); // boom :(

I wonder if the correct solution is to have a code check (at the EBN) that 
smartpointers aren't copy in a C++ normal pointer.

-- 
--- Cyrille Berger ---




More information about the kde-core-devel mailing list