home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12640 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  2.9 KB

  1. Path: sparky!uunet!centerline!noc.near.net!hri.com!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Reference counting for vectors, (Tony Hansen's book).
  5. Message-ID: <TMB.92Aug20152400@arolla.idiap.ch>
  6. Date: 20 Aug 92 19:24:00 GMT
  7. References: <TMB.92Aug16160535@arolla.idiap.ch> <714264851snx@trmphrst.demon.co.uk>
  8. Sender: news@ai.mit.edu
  9. Reply-To: tmb@idiap.ch
  10. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  11.     Perceptive)
  12. Lines: 59
  13. In-reply-to: nikki@trmphrst.demon.co.uk's message of 19 Aug 92 15:54:11 GMT
  14.  
  15. In article <714264851snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
  16.  
  17.    [quoting a template class to do reference counting that I posted]
  18.  
  19.    I think this copy operator contains the classic copy operator bug - try 
  20.    assigning a Counted object to itself - the object will be deleted before 
  21.    copying. Try ...
  22.  
  23.        Counted &operator=(Counted &other) {
  24.            other.acquire();
  25.            drop();
  26.            count=other.count;
  27.            object=other.object;
  28.            return *this;
  29.        }
  30.  
  31. You are right; this is a better definition (the last reference counted
  32. class that I wrote, about 5 years ago, got this right...).
  33.  
  34.    Mind you, there is still a bug here - try 
  35.  
  36.        Counted<Thing> a, b;
  37.  
  38.        a = b;        // core dump ?
  39.  
  40.    The fix is for acquire to check count is not NULL before doing (*count)++;
  41.  
  42. I don't think this is necessarily a bug. Assigning an uninitialized
  43. reference counted object is not necessarily guaranteed to work...
  44.  
  45.    The original author clearly stated that the program was not tested, but I 
  46.    thought I ought to point out these problems so that others didn't copy the
  47.    mistakes.
  48.  
  49.    Mind you, I haven't even compiled the code, let alone tested it, so I am 
  50.    even more likely to be wrong ! 
  51.  
  52.    [ Hobby horse of the week ... ]
  53.    However, if I am right, think about how useful code review is in picking 
  54.    up such obscure bugs, and how cheap compared to hours of testing !
  55.  
  56. It also shows the value of defensive programming: because I was
  57. careful to zero out various fields in the structure upon destruction,
  58. any of these problems would have been caught on the machine I'm using.
  59. Such practices have saved my neck more than once (I can't afford the
  60. luxury of code review, unless someone volunteers...).
  61.  
  62. Anyway, this was mainly a toy that I typed in to show that you can use
  63. template classes, to a limited degree, to implement reference
  64. counting. I don't recommend actually using it. The overhead of
  65. reference counting is significant (in particular, when implemented as
  66. a template class). I have not used it in years...
  67.  
  68. It turns out that if you could distinguish stack from heap objects,
  69. you could even implement mark-and-sweep garbage collection as a
  70. template class--albeit at a runtime cost (in both time and space) that
  71. is probably even higher than that of reference counting.
  72.  
  73.                     Thomas.
  74.