CCRef.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCRef.h"
  23. #include "base/CCAutoreleasePool.h"
  24. #include "base/ccMacros.h"
  25. #if CC_REF_LEAK_DETECTION
  26. #include <algorithm> // std::find
  27. #endif
  28. NS_CC_BEGIN
  29. #if CC_REF_LEAK_DETECTION
  30. static void trackRef(Ref* ref);
  31. static void untrackRef(Ref* ref);
  32. #endif
  33. Ref::Ref()
  34. : _referenceCount(1) // when the Ref is created, the reference count of it is 1
  35. {
  36. #if CC_REF_LEAK_DETECTION
  37. trackRef(this);
  38. #endif
  39. }
  40. Ref::~Ref()
  41. {
  42. #if CC_REF_LEAK_DETECTION
  43. if (_referenceCount != 0)
  44. untrackRef(this);
  45. #endif
  46. }
  47. void Ref::retain()
  48. {
  49. CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
  50. ++_referenceCount;
  51. }
  52. void Ref::release()
  53. {
  54. CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
  55. --_referenceCount;
  56. if (_referenceCount == 0)
  57. {
  58. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  59. auto poolManager = PoolManager::getInstance();
  60. if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
  61. {
  62. // Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
  63. // This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
  64. //
  65. // Wrong usage (1):
  66. //
  67. // auto obj = Node::create(); // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
  68. // obj->autorelease(); // Wrong: If you wish to invoke autorelease several times, you should retain `obj` first.
  69. //
  70. // Wrong usage (2):
  71. //
  72. // auto obj = Node::create();
  73. // obj->release(); // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
  74. //
  75. // Correct usage (1):
  76. //
  77. // auto obj = Node::create();
  78. // |- new Node(); // `new` is the pair of the `autorelease` of next line
  79. // |- autorelease(); // The pair of `new Node`.
  80. //
  81. // obj->retain();
  82. // obj->autorelease(); // This `autorelease` is the pair of `retain` of previous line.
  83. //
  84. // Correct usage (2):
  85. //
  86. // auto obj = Node::create();
  87. // obj->retain();
  88. // obj->release(); // This `release` is the pair of `retain` of previous line.
  89. CCASSERT(false, "The reference shouldn't be 0 because it is still in autorelease pool.");
  90. }
  91. #endif
  92. #if CC_REF_LEAK_DETECTION
  93. untrackRef(this);
  94. #endif
  95. delete this;
  96. }
  97. }
  98. Ref* Ref::autorelease()
  99. {
  100. PoolManager::getInstance()->getCurrentPool()->addObject(this);
  101. return this;
  102. }
  103. unsigned int Ref::getReferenceCount() const
  104. {
  105. return _referenceCount;
  106. }
  107. #if CC_REF_LEAK_DETECTION
  108. static std::list<Ref*> __refAllocationList;
  109. void Ref::printLeaks()
  110. {
  111. // Dump Ref object memory leaks
  112. if (__refAllocationList.empty())
  113. {
  114. log("[memory] All Ref objects successfully cleaned up (no leaks detected).\n");
  115. }
  116. else
  117. {
  118. log("[memory] WARNING: %d Ref objects still active in memory.\n", (int)__refAllocationList.size());
  119. for (const auto& ref : __refAllocationList)
  120. {
  121. CC_ASSERT(ref);
  122. const char* type = typeid(*ref).name();
  123. log("[memory] LEAK: Ref object '%s' still active with reference count %d.\n", (type ? type : ""), ref->getReferenceCount());
  124. }
  125. }
  126. }
  127. static void trackRef(Ref* ref)
  128. {
  129. CCASSERT(ref, "Invalid parameter, ref should not be null!");
  130. // Create memory allocation record.
  131. __refAllocationList.push_back(ref);
  132. }
  133. static void untrackRef(Ref* ref)
  134. {
  135. auto iter = std::find(__refAllocationList.begin(), __refAllocationList.end(), ref);
  136. if (iter == __refAllocationList.end())
  137. {
  138. log("[memory] CORRUPTION: Attempting to free (%s) with invalid ref tracking record.\n", typeid(*ref).name());
  139. return;
  140. }
  141. __refAllocationList.erase(iter);
  142. }
  143. #endif // #if CC_REF_LEAK_DETECTION
  144. NS_CC_END