Re: To use or not to use smart pointers?
On Jul 25, 3:44 pm, Boris <bo...@gtemail.net> wrote:
On Tue, 24 Jul 2007 11:55:19 +0200, James Kanze <james.ka...@gmail.com>
wrote:
[...]
[...]And my experience is that automatically using just about any
tool results in misuse, and poor code quality. Most pointers in
my code are raw pointers, simply because most pointers are used
for navigation, in one way or another. Most "objects" are local
How do you make sure though that your pointers are not in fact
dangling pointers? Is it something the Boehm collector takes
care of?
Not directly. It does ensures that the memory won't be reused
for anything else as long as you have a pointer to it, so it's
possible to add a flag to the memory, which you assert each time
you use the pointer. But in general, you still need to use the
observer pattern, and notify all clients of the object anytime
the object ceases to exist.
I see. I don't think though that using the observer pattern makes more
sense than using smart pointers.
If smart pointers could take care of it, it doesn't. Typically,
however, the class having the pointer needs to react in some
way: remove the actual pointer from a container, or often, find
an alternative resource, or pass into a degraded functional
mode, or something along those lines. The problem is that the
reaction is too application specific to be easily handled by a
smart pointer. (Or is it? I've not given it a try, but I can
sort of imagine a smart pointer which understands something
about the container which contains it, and removes itself when
notified that the pointed to object has ceased to exist.)
Not only need all classes to support the
observer pattern for memory management. You have also a more tight
coupling than with smart pointers as A does not only own B anymore but
must register with B.
Again, if a smart pointer actually fits the bill, so much the
better. My experience has been that they rarely do.
I feel like however arguing with the wrong person as
I appreciate ideas like using garbage collectors in C++ to make memory
management easier. Developers refusing to use smart pointers because of
performance concerns might be however even less willing to use gargabe
collectors. :)
:-) Probably. For many applications, garbage collection will
actually be faster than either smart pointers or manual memory
management. But people who reject the smart pointer solution
out of hand for imagined performance issues will doubtlessly do
the same for garbage collection. (It's interesting to note that
in the presence of polymorphism, at least as it is typically
implemented in C++, a dangling pointer can result in the same
type of security hole as a buffer overflow. Garbage collection,
of course, eliminates this:-). It doesn't make an incorrect
program correct, but it does limit the dammage that can be done
because of the error.)
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34