Ref.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "Base.h"
  2. #include "Ref.h"
  3. #include "Game.h"
  4. namespace gameplay
  5. {
  6. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  7. void* trackRef(Ref* ref);
  8. void untrackRef(Ref* ref, void* record);
  9. #endif
  10. Ref::Ref() :
  11. _refCount(1)
  12. {
  13. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  14. __record = trackRef(this);
  15. #endif
  16. }
  17. Ref::Ref(const Ref& copy) :
  18. _refCount(1)
  19. {
  20. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  21. __record = trackRef(this);
  22. #endif
  23. }
  24. Ref::~Ref()
  25. {
  26. }
  27. void Ref::addRef()
  28. {
  29. ++_refCount;
  30. }
  31. void Ref::release()
  32. {
  33. if ((--_refCount) <= 0)
  34. {
  35. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  36. untrackRef(this, __record);
  37. #endif
  38. delete this;
  39. }
  40. }
  41. unsigned int Ref::getRefCount() const
  42. {
  43. return _refCount;
  44. }
  45. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  46. struct RefAllocationRecord
  47. {
  48. Ref* ref;
  49. RefAllocationRecord* next;
  50. RefAllocationRecord* prev;
  51. };
  52. RefAllocationRecord* __refAllocations = 0;
  53. int __refAllocationCount = 0;
  54. void Ref::printLeaks()
  55. {
  56. // Dump Ref object memory leaks
  57. if (__refAllocationCount == 0)
  58. {
  59. printError("[memory] All Ref objects successfully cleaned up (no leaks detected).\n");
  60. }
  61. else
  62. {
  63. printError("[memory] WARNING: %d Ref objects still active in memory.\n", __refAllocationCount);
  64. for (RefAllocationRecord* rec = __refAllocations; rec != NULL; rec = rec->next)
  65. {
  66. Ref* ref = rec->ref;
  67. GP_ASSERT(ref);
  68. const char* type = typeid(*ref).name();
  69. printError("[memory] LEAK: Ref object '%s' still active with reference count %d.\n", (type ? type : ""), ref->getRefCount());
  70. }
  71. }
  72. }
  73. void* trackRef(Ref* ref)
  74. {
  75. GP_ASSERT(ref);
  76. // Create memory allocation record.
  77. RefAllocationRecord* rec = (RefAllocationRecord*)malloc(sizeof(RefAllocationRecord));
  78. rec->ref = ref;
  79. rec->next = __refAllocations;
  80. rec->prev = 0;
  81. if (__refAllocations)
  82. __refAllocations->prev = rec;
  83. __refAllocations = rec;
  84. ++__refAllocationCount;
  85. return rec;
  86. }
  87. void untrackRef(Ref* ref, void* record)
  88. {
  89. if (!record)
  90. {
  91. printError("[memory] ERROR: Attempting to free null ref tracking record.\n");
  92. return;
  93. }
  94. RefAllocationRecord* rec = (RefAllocationRecord*)record;
  95. if (rec->ref != ref)
  96. {
  97. printError("[memory] CORRUPTION: Attempting to free Ref with invalid ref tracking record.\n");
  98. return;
  99. }
  100. // Link this item out.
  101. if (__refAllocations == rec)
  102. __refAllocations = rec->next;
  103. if (rec->prev)
  104. rec->prev->next = rec->next;
  105. if (rec->next)
  106. rec->next->prev = rec->prev;
  107. free((void*)rec);
  108. --__refAllocationCount;
  109. }
  110. #endif
  111. }