From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!watmath!clyde!att!alberta!ubc-cs!uw-beaver!cornell!batcomputer!itsgw!steinmetz!uunet!mcvax!enea!sommar From: sommar@enea.se (Erland Sommarskog) Newsgroups: comp.lang.ada Subject: Re: Garbage Collection Message-ID: <4195@enea.se> Date: 26 Dec 88 23:37:58 GMT Organization: ENEA DATA AB, Sweden List-Id: Bill Wolfe (wtwolfe@hubcap.UUCP) first said: >>> The deallocation of every object in the local environment is >>> performed as an automatic service when a procedure, function, >>> or local block is exited. This is not garbage collection, >>> because the programmer has implicitly directed that the >>> destruction be performed. I tried to understand: >> I read this as "on block exit all memory allocated to variables >> declared in that block should be deallocated". >> Isn't this very dangerous? What if programmer copied the object to >> a variable declared in a outer block? Or stored it in a table of some >> sort in a subprogram call made in block? And Bill explains: > Not at all. If a programmer assigns the value stored in some local > object to some nonlocal object, then we simply have a new value for > a nonlocal object, assuming we have not engaged in the foul practice > of structural sharing. Those who engage in structural sharing will > get the run-time errors they deserve for engaging in such space-hacking. And I still don't understand. Let's say we have a tree handler Generic Data_type is limited private; Procedure -- Assign, "<" and ">" Package Tree_handler Tree_type is private; Assume further that we instantiate this package with Some_access_type, it could be an imported limited type, it could be a locally defined. Then we have the procedure: Procedure Insert_data_to_tree(Tree : in out Tree_type; Data_of_some_sort : in Any_type); Reference : Some_access_type; Begin Reference := new Some_type; -- Or subprogram call for external type Prepare(Reference, Data_of_some_sort); Insert(Tree, Reference); End Insert_data_to_tree; According to Bill the data allocated to Reference should be freed when we exit this procedure, but if Insert does what we think this is of course a severe error. Where did I go wrong? Did I plead guilty to structure sharing just because I inserted a pointer to a tree? Or does Bill mean that when Reference is inserted to the tree that Reference.all should be inserted to the tree, not the pointer itself? But what if I also insert the reference into another tree (or some other structure) and then modify the referred data (some counter, for instance) and want this change to appear in both places? Note also that in this example, there are no side effects. We are only dealing with local variables and paramerters. And still deallocation on block exit is completely wrong. (And there is no way the compiler could deduce that Insert really saves Reference somewhere.) Several times in this discussion Bill Wolfe has claimed that garbage collection is a trouble-maker for maintainers. In what way? I can only see one: If the system due to a heavy use of memory is spending a lot of time on garbage collecttion, something must be done. But in this case the problem is often quite easy to spot. Much worse for maintainence is when the system dies with "System access violation" or "segmentation fault" from time to time and you don't know why. The reason may be that an allocated area was erroneously released and then reused and the original data, that someone appearently is relying in, has been over-written. Or because an already deallocated was deallocated a second time. Bill Wolfe is trying to outline some alternative, but I think he has to think more about, until it is equally safe. (And really, I don't think he should. He is just reinventing the wheel.) Bill Wolfe said earlier that garbage collection encourages sloppy programming. Whether it is sloppy or not, I don't care, but obviously garbage collection is taking a lot of the burden of the programmer's shoulder and the maintainer's too. Why should a programmer waste time bothering about something that the computer could do much safer for him? After all, the programmer does not work for free. So to conclude: I think Ada should require garbage collection that could be disabled with a pragma for critical applications. As it is now a portable application cannot rely on it, with all the troubles explicit deallocation implies. -- Erland Sommarskog ENEA Data, Stockholm This signature is not to be quoted. sommar@enea.se