STABLIZE.CXX
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:6k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       stablize.cxx
  7. //
  8. //  Contents:   Stabilization Classes used to stabilize objects during
  9. //              re-entrant calls.
  10. //
  11. //  Classes:    CSafeRefCount
  12. //              CStabilize
  13. //
  14. //  History:    8-26-94   stevebl   Modified from code written by AlexGo
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <windows.h>
  18. #include <ole2.h>
  19. #include <stablize.h>
  20. #include "assert.h"
  21. //+-------------------------------------------------------------------------
  22. //
  23. //  Member:     CSafeRefCount::CSafeRefCount
  24. //
  25. //  Synopsis:   constructor for the safe ref count class
  26. //
  27. //  History:    dd-mmm-yy Author    Comment
  28. //              28-Jul-94 alexgo    author
  29. //              8-26-94   stevebl   modified for use by SDK samples
  30. //
  31. //--------------------------------------------------------------------------
  32. CSafeRefCount::CSafeRefCount()
  33. {
  34.         m_cRefs = 0;
  35.         m_cNest = 0;
  36.         m_fInDelete = FALSE;
  37. }
  38. //+-------------------------------------------------------------------------
  39. //
  40. //  Member:     CSafeRefCount::~CSafeRefCount (virtual)
  41. //
  42. //  History:    dd-mmm-yy Author    Comment
  43. //              28-Jul-94 alexgo    author
  44. //              8-26-94   stevebl   modified for use by SDK samples
  45. //
  46. //--------------------------------------------------------------------------
  47. CSafeRefCount::~CSafeRefCount()
  48. {
  49.         assert(m_cRefs == 0 && m_cNest == 0 && m_fInDelete == TRUE);
  50. }
  51. //+-------------------------------------------------------------------------
  52. //
  53. //  Member:     CSafeRefCount::SafeAddRef
  54. //
  55. //  Synopsis:   increments the reference count on the object
  56. //
  57. //  Returns:    ULONG -- the reference count after the increment
  58. //
  59. //  History:    dd-mmm-yy Author    Comment
  60. //              28-Jul-94 alexgo    author
  61. //              8-26-94   stevebl   modified for use by SDK samples
  62. //
  63. //--------------------------------------------------------------------------
  64. ULONG CSafeRefCount::SafeAddRef()
  65. {
  66.         m_cRefs++;
  67.         return m_cRefs;
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. //  Member:     CSafeRefCount::SafeRelease
  72. //
  73. //  Synopsis:   decrements the reference count on the object
  74. //
  75. //  Effects:    May delete the object!
  76. //
  77. //  Returns:    ULONG -- the reference count after decrement
  78. //
  79. //  Algorithm:  decrements the reference count.  If the reference count
  80. //              is zero AND the nest count is zero AND we are not currently
  81. //              trying to delete our object, then it is safe to delete.
  82. //
  83. //  History:    dd-mmm-yy Author    Comment
  84. //              28-Jul-94 alexgo    author
  85. //              8-26-94   stevebl   modified for use by SDK samples
  86. //
  87. //--------------------------------------------------------------------------
  88. ULONG CSafeRefCount::SafeRelease()
  89. {
  90.         ULONG   cRefs;
  91.         if( m_cRefs > 0 )
  92.         {
  93.                 cRefs = --m_cRefs;
  94.                 if( m_cRefs == 0 && m_cNest == 0 && m_fInDelete == FALSE )
  95.                 {
  96.                         m_fInDelete = TRUE;
  97.                         delete this;
  98.                 }
  99.         }
  100.         else
  101.         {
  102.                 // somebody is releasing a non-addrefed pointer!!
  103.                 assert(NULL == "Release called on a non-addref'ed pointer!n");
  104.                 cRefs = 0;
  105.         }
  106.         return cRefs;
  107. }
  108. //+-------------------------------------------------------------------------
  109. //
  110. //  Member:     CSafeRefCount::IncrementNestCount
  111. //
  112. //  Synopsis:   increments the nesting count of the object
  113. //
  114. //  Arguments:  none
  115. //
  116. //  Returns:    ULONG; the nesting count after increment
  117. //
  118. //  History:    dd-mmm-yy Author    Comment
  119. //              28-Jul-94 alexgo    author
  120. //              8-26-94   stevebl   modified for use by SDK samples
  121. //
  122. //  Notes:      The nesting count is the count of how many times an
  123. //              an object has been re-entered.  For example, suppose
  124. //              somebody calls pFoo->Bar1(), which makes some calls that
  125. //              eventually call pFoo->Bar2();.  On entrace to Bar2, the
  126. //              nest count of the object should be 2 (since the invocation
  127. //              of Bar1 is still on the stack above us).
  128. //
  129. //              It is important to keep track of the nest count so we do
  130. //              not accidentally delete ourselves during a nested invocation.
  131. //              If we did, then when the stack unwinds to the original
  132. //              top level call, it could try to access a non-existent member
  133. //              variable and crash.
  134. //
  135. //--------------------------------------------------------------------------
  136. ULONG CSafeRefCount::IncrementNestCount()
  137. {
  138.         m_cNest++;
  139.         return m_cNest;
  140. }
  141. //+-------------------------------------------------------------------------
  142. //
  143. //  Member:     CSafeRefCount::DecrementNestCount
  144. //
  145. //  Synopsis:   decrements the nesting count and deletes the object
  146. //              (if necessary)
  147. //
  148. //  Effects:    may delete 'this' object!
  149. //
  150. //  Arguments:  none
  151. //
  152. //  Returns:    ULONG, the nesting count after decrement
  153. //
  154. //  Algorithm:  decrements the nesting count.  If the nesting count is zero
  155. //              AND the reference count is zero AND we are not currently
  156. //              trying to delete ourselves, then delete 'this' object
  157. //
  158. //  History:    dd-mmm-yy Author    Comment
  159. //              28-Jul-94 alexgo    author
  160. //              8-26-94   stevebl   modified for use by SDK samples
  161. //
  162. //  Notes:
  163. //
  164. //--------------------------------------------------------------------------
  165. ULONG CSafeRefCount::DecrementNestCount()
  166. {
  167.         ULONG   cNest;
  168.         if( m_cNest > 0 )
  169.         {
  170.                 cNest = --m_cNest;
  171.                 if( m_cRefs == 0 && m_cNest == 0 && m_fInDelete == FALSE )
  172.                 {
  173.                         m_fInDelete = TRUE;
  174.                         delete this;
  175.                 }
  176.         }
  177.         else
  178.         {
  179.                 // somebody forget to increment the nest count!!
  180.                 assert(NULL == "Unbalanced nest count!!");
  181.                 cNest = 0;
  182.         }
  183.         return cNest;
  184. }