Ref.cpp 2.7 KB

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