about kde4's smart pointer

Michel Hermier michel.hermier at gmail.com
Wed Oct 4 11:38:56 BST 2006


2006/10/4, Cyrille Berger <cberger at cberger.net>:
> > 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.

If someone write a rock solid test, I'll think about removing the explicit.
But I wonder if that will not conflict with the explicit constructor check.
smartpointer's and regular pointer is bad.




More information about the kde-core-devel mailing list