Ref.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. const char* type = typeid(*ref).name();
  68. printError("[memory] LEAK: Ref object '%s' still active with reference count %d.\n", (type ? type : ""), ref->getRefCount());
  69. }
  70. }
  71. }
  72. void* trackRef(Ref* ref)
  73. {
  74. // Create memory allocation record
  75. RefAllocationRecord* rec = (RefAllocationRecord*)malloc(sizeof(RefAllocationRecord));
  76. rec->ref = ref;
  77. rec->next = __refAllocations;
  78. rec->prev = 0;
  79. if (__refAllocations)
  80. __refAllocations->prev = rec;
  81. __refAllocations = rec;
  82. ++__refAllocationCount;
  83. return rec;
  84. }
  85. void untrackRef(Ref* ref, void* record)
  86. {
  87. RefAllocationRecord* rec = (RefAllocationRecord*)record;
  88. if (rec->ref != ref)
  89. {
  90. printError("[memory] CORRUPTION: Attempting to free Ref with invalid ref tracking record.\n");
  91. return;
  92. }
  93. // Link this item out
  94. if (__refAllocations == rec)
  95. __refAllocations = rec->next;
  96. if (rec->prev)
  97. rec->prev->next = rec->next;
  98. if (rec->next)
  99. rec->next->prev = rec->prev;
  100. free((void*)rec);
  101. --__refAllocationCount;
  102. }
  103. #endif
  104. }