B
Boris
[...]What I don't like about using smart pointers systematically (as
opposed to using them as a solution to a specific problem in
specific cases) is that they force an object lifetime model on
you, which in most cases is not the right one. As I said
earlier, most pointers are for navigation. You do NOT,
That makes perfectly sense to me as you can clearly distinguish two
concepts: Thanks to smart pointers raw pointers are used to indicate that
they don't own memory. However I typically use then references (which
doesn't work of course if you need to change the pointer somehow).
[...]I don't know how COM handles this, but the normal convention is
that if you receive a raw pointer, you don't have to worry about
lifetime. Too much. If you have a pointer to an entity object
There is no real solution in the COM world except looking up the MSDN
documentation all the time (or you can say .NET is the solution to this
and other problems in the COM world).
[...][...]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 localHow 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. 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. 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.
Boris