home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12590 < prev    next >
Encoding:
Text File  |  1992-08-19  |  2.7 KB  |  110 lines

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Distribution: world
  5. Subject: Re: Reference counting for vectors, (Tony Hansen's book).
  6. References: <TMB.92Aug16160535@arolla.idiap.ch>
  7. X-Mailer: cppnews $Revision: 1.14 $
  8. Organization: Trumphurst Ltd.
  9. Lines: 96
  10. Date: Wed, 19 Aug 1992 15:54:11 +0000
  11. Message-ID: <714264851snx@trmphrst.demon.co.uk>
  12. Sender: usenet@gate.demon.co.uk
  13.  
  14.  
  15. In article <TMB.92Aug16160535@arolla.idiap.ch> tmb@idiap.ch writes:
  16.  
  17. > Counted.h
  18. > #ifndef __COUNTED__
  19. > #define __COUNTED__
  20. > template <class T>
  21. > struct Counted {
  22. >  private:
  23. >     int *count;
  24. >     T *object;
  25. >     void drop() {
  26. >         if(!count) return;
  27. >         (*count)--;
  28. >         if(*count<=0) {
  29. >             delete count;
  30. >             delete object;
  31. >             count=0;
  32. >             object=0;
  33. >         }
  34. >     }
  35. >     void acquire() {
  36. >         (*count)++;
  37. >     }
  38. >  public:
  39. >     Counted() {
  40. >         count=0;
  41. >         object=0;
  42. >     }
  43. >     Counted(T *object):object(object) {
  44. >         count = new int(1);
  45. >     }
  46. >     ~Counted() {
  47. >         drop();
  48. >     }
  49. >     Counted(Counted &other) {
  50. >         count=other.count;
  51. >         object=other.object;
  52. >         acquire();
  53. >     }
  54. >     Counted &operator=(Counted &other) {
  55. >         drop();
  56. >         count=other.count;
  57. >         object=other.object;
  58. >         acquire();
  59. >         return *this;
  60. >     }
  61. I think this copy operator contains the classic copy operator bug - try 
  62. assigning a Counted object to itself - the object will be deleted before 
  63. copying. Try ...
  64.  
  65.     Counted &operator=(Counted &other) {
  66.         other.acquire();
  67.         drop();
  68.         count=other.count;
  69.         object=other.object;
  70.         return *this;
  71.     }
  72. Mind you, there is still a bug here - try 
  73.  
  74.     Counted<Thing> a, b;
  75.  
  76.     a = b;        // core dump ?
  77.  
  78. The fix is for acquire to check count is not NULL before doing (*count)++;
  79.  
  80. >     T &operator*() {return *object;}
  81. >     T *operator->() {return object;}
  82. >     operator T() {return *object;}
  83. >     
  84. >     // do _not_ provide "operator T*()"
  85. > };
  86. > #endif
  87.  
  88. The original author clearly stated that the program was not tested, but I 
  89. thought I ought to point out these problems so that others didn't copy the
  90. mistakes.
  91.  
  92. Mind you, I haven't even compiled the code, let alone tested it, so I am 
  93. even more likely to be wrong ! 
  94.  
  95. [ Hobby horse of the week ... ]
  96. However, if I am right, think about how useful code review is in picking 
  97. up such obscure bugs, and how cheap compared to hours of testing !
  98. ---
  99. Nikki Locke              |                        | nikki@trmphrst.demon.co.uk
  100. Trumphurst Ltd.          | Tel: +44 (0)691-670318 | nikki@cix.compulink.co.uk
  101. PC and Unix consultancy  | Fax: +44 (0)691-670316 | nikki@kewill.co.uk
  102. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  103. Demon.co.uk is a dial-up subscription access point to the Internet.
  104.