Re: writing robust software?
On Sep 15, 5:27 pm, Frank Buss <f...@frank-buss.de> wrote:
Daniel Kr=FCgler wrote:
What about the combination of shared_ptr and
weak_ptr?
This is possible, but you still can create circles in complex
object diagrams, if you forget to use weak_ptr at the right
places, but it would be better than plain pointers. Do you
think it is a good idea to rewrite an application to use
shared_ptr and weak_ptr, only, instead of plain pointers?
I think I have already bad memory leaks, when I some parts of
my application are executed. But maybe I can find it with
Valgrind, which would be more easy instead of rewriting lots
of the application.
In general, smart pointers are not the silver bullet other
posters seem to suggest. They are convenient for some specific
cases (e.g. giving value semantics, or something similar, to
polymorphic objects), but most of the time, you shouldn't be
using dynamically allocated memory at all unless the object in
question has a specific, deterministic lifetime. (And if the
object has a specific, deterministic lifetime, you know when to
delete it, no?)
In my experience, dangling pointers cause more problems than
leaking memory. The object has been destructed, because the
program logic require that it be destructed at that time, but
somewhere, some code wasn't informed. The only practical
solution I've found for this is to use the Boehm collector: set
a flag when the object is destructed, and assert that the flag
isn't set at the start of each function. (The Boehm collector
ensures that the memory underlying the object will not be
recycled for some other object as long as there are any pointers
to it.)
--
James Kanze
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]