Re: Garbage Collection - The Trash Begins To Pile Up
Nevin :-] Liber wrote:
In article <Z76dnR73I66gygTYnZ2dnUVZ_oipnZ2d@comcast.com>,
Walter Bright <walter@digitalmars-nospamm.com> wrote:
For the potential problem with the free store, just throw an exception:
Object *o = new Object();
foo(); // foo() throws an exception, o is now leaked
return o;
And how does does GC magically fix this code?
There are two bugs here: the memory leak, and that Object may never be
destroyed. GC can only address the first one.
It addresses both, because most destructors exist for the sole purpose
of cleaning up memory. Using GC eliminates the need for most
destructors. For non-memory resources, GC is the wrong solution - but we
were talking about free store issues here.
If I write the code as:
std::auto_ptr<Object> o(new Object());
foo(); // foo() throws an exception, o is NOT leaked
return o;
I don't have the leak, the non-destruction bug, nor the tight coupling.
If auto_ptr<> was the answer, C++ wouldn't have the endless bestiary of
all kinds of xxx_ptr<> templates, each with their own pros, cons, and
potential bugs.
And every time you use GC you have the potential of a non-destruction
bug.
Don't get me wrong; I want GC. But this is a real issue with it.
I've said before, and I'll repeat. GC is the wrong solution for
management of non-memory resources. Most of the troubles people have
with GC stem from misapplying it to such resources.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]