1 // ObjectTracker.h 2 3 #include <new> 4 #include <typeinfo> 5 6 #include <AutoLocker.h> 7 8 #include "Debug.h" 9 #include "ObjectTracker.h" 10 11 static char sTrackerBuffer[sizeof(ObjectTracker)]; 12 13 // constructor 14 ObjectTrackable::ObjectTrackable() 15 { 16 ObjectTracker::GetDefault()->AddTrackable(this); 17 } 18 19 // destructor 20 ObjectTrackable::~ObjectTrackable() 21 { 22 ObjectTracker::GetDefault()->RemoveTrackable(this); 23 } 24 25 26 // #pragma mark - 27 28 // constructor 29 ObjectTracker::ObjectTracker() 30 : fLock("object tracker"), 31 fTrackables() 32 { 33 } 34 35 // destructor 36 ObjectTracker::~ObjectTracker() 37 { 38 ObjectTrackable* trackable = fTrackables.First(); 39 if (trackable) { 40 WARN(("ObjectTracker: WARNING: There are still undeleted objects:\n")); 41 for (; trackable; trackable = fTrackables.GetNext(trackable)) { 42 WARN((" trackable: %p: type: `%s'\n", trackable, 43 typeid(*trackable).name())); 44 } 45 } 46 } 47 48 // InitDefault 49 ObjectTracker* 50 ObjectTracker::InitDefault() 51 { 52 if (!sTracker) 53 sTracker = new(sTrackerBuffer) ObjectTracker; 54 return sTracker; 55 } 56 57 // ExitDefault 58 void 59 ObjectTracker::ExitDefault() 60 { 61 if (sTracker) { 62 sTracker->~ObjectTracker(); 63 sTracker = NULL; 64 } 65 } 66 67 // GetDefault 68 ObjectTracker* 69 ObjectTracker::GetDefault() 70 { 71 return sTracker; 72 } 73 74 // AddTrackable 75 void 76 ObjectTracker::AddTrackable(ObjectTrackable* trackable) 77 { 78 if (!this) 79 return; 80 81 if (trackable) { 82 AutoLocker<Locker> _(fLock); 83 fTrackables.Insert(trackable); 84 } 85 } 86 87 // RemoveTrackable 88 void 89 ObjectTracker::RemoveTrackable(ObjectTrackable* trackable) 90 { 91 if (!this) 92 return; 93 94 if (trackable) { 95 AutoLocker<Locker> _(fLock); 96 fTrackables.Remove(trackable); 97 } 98 } 99 100 // sTracker 101 ObjectTracker* ObjectTracker::sTracker = NULL; 102 103