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