home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13033 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  2.5 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!dptg!ulysses!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <23580@alice.att.com>
  6. Date: 28 Aug 92 15:40:39 GMT
  7. References: <DAVEG.92Aug20043156@synaptx.synaptics.com> <28623@vedge.UUCP> <DAVEG.92Aug28004331@synaptx.synaptics.com>
  8. Reply-To: ark@alice.UUCP ()
  9. Organization: AT&T Bell Laboratories, Murray Hill NJ
  10. Lines: 47
  11.  
  12. In article <DAVEG.92Aug28004331@synaptx.synaptics.com> daveg@synaptics.com (Dave Gillespie) writes:
  13.  
  14. > Your description is more dramatic than clear :-), but it brings to
  15. > mind an example where garbage collection could be troublesome in C++.
  16. > Is this what you were thinking of?
  17.  
  18. >    class D *orphanedThing;
  19. >    class C {
  20. >      D *thing;
  21. >      ~C() { orphanedThing = thing; }
  22. >    };
  23.  
  24. That is indeed a troublesome case for GC in C++.
  25.  
  26. I think the right answer, at least for most practical purposes, is to
  27. say that the purpose of garbage collection is to simulate unbounded
  28. memory.  Once an object has become garbage, there is no way to tell
  29. whether that memory is still in use, so the implementation should be
  30. allowed to reclaim it without explicitly destroying the object.
  31. As a programmer, you shouldn't care; you have no way of finding out that
  32. it happened.
  33.  
  34. Now, of course, there is a problem here: people may well want garbage
  35. collection to be not quite so transparent.  Suppose, for example, that
  36. an object holds some kind of resource, such as a file descriptor.  It
  37. would be nice to release that resource, too, after the object is no
  38. longer visible.
  39.  
  40. However, I don't think it's right to do this in the garbage collector:
  41. if you have enough physical memory, GC may not occur for a long time.
  42. Imagine, for example, a window system that relies on GC to remove windows
  43. whose associated process has died.  You want those windows to go away
  44. *now*, not after an indeterminate interval.
  45.  
  46. I have seen one compromise position, though, that might conceivably
  47. be useful for some of these problems: the notion of a `weak pointer.'
  48. A weak pointer points at an object as long as the object is still
  49. around; when the only pointers to an object are weak pointers, the
  50. object is deleted and all the weak pointers are set to zero.  You can
  51. then test the weak pointer at some future time and see that the object
  52. has vanished without the kinds of circularites implied by the earler
  53. example.
  54.  
  55. Would weak pointers be useful?  I'm still not convinced one way or the other.
  56. -- 
  57.                 --Andrew Koenig
  58.                   ark@europa.att.com
  59.