Re: If GC is the solution, then what is the problem?
Peter C. Chapin wrote:
James Kanze wrote:
Garbage collection is about managing memory. If the only
reason you are calling delete is to free the memory (the most
frequent case in my code), then you don't need it. If you
are calling it for other reasons (i.e. the destructor of the
object does more than just free memory), then you need it.
Obviously, this means distinguishing between categories of
objects. But we already have do that anyway, so there's no
real additional constraint here. About the only difference
is that with garbage collection, there is less code to write,
and less code to debug.
What if the category of an object changed during program
maintenance?
You mean you do a major redesign, in which a class changes its
role. Then you obviously have to reconsider every use of the
class. Garbage collection or no.
(Most of the places I've worked have had a rule that if you
change something that much, you change its name, in order to
ensure that every place the class is used is reworked to conform
to the new class specifications.)
For example at first a class might only use its destructor to
release memory but then later it is revised to do other
necessary (non-memory) resource management tasks. If the
program was relying on GC to clean up objects from this class,
such a change would require a very error prone process of
adding deletes everywhere they were previously left out.
As would just about any other solution you could envisage. If
you change the contract of a class, then every single use in the
entire program must be carefully reexamined, to ensure that the
new specifications of the class are appropriate. The usual
technique to ensure that this is done is to change the name.
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]