Skip to content

Commit

Permalink
Report tracked objects at exit
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Nov 29, 2024
1 parent a1a3f42 commit e6b425d
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions Source/Additions/NSObject+GNUstepBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ + (void) _endThread: (NSThread*)thread;
static BOOL isExiting = NO;
static NSLock *exitLock = nil;

struct trackLink {
struct trackLink *next;
id object; // Instance or Class being tracked.
IMP dealloc; // Original -dealloc implementation
IMP release; // Original -release implementation
IMP retain; // Original -retain implementation
BOOL global; // If all instance are tracked.
BOOL instance; // If the object is an instance.
};

static struct trackLink *tracked = 0;
static gs_mutex_t trackLock = GS_MUTEX_INIT_STATIC;

static inline void setup()
{
if (nil == exitLock)
Expand Down Expand Up @@ -245,6 +258,27 @@ static inline void setup()
[[NSAutoreleasePool currentPool] dealloc];
[NSAutoreleasePool _endThread: GSCurrentThread()];
}

/* Exit/clean-up done ... we can get rid of tracking data too.
*/
if (tracked)
{
GS_MUTEX_LOCK(trackLock);
while (tracked)
{
struct trackLink *next = tracked->next;

if (tracked->instance)
{
fprintf(stderr, "Tracking ownership -[%p dealloc]"
" not called by exit.\n", tracked->object);
}
free(tracked);
tracked = next;
}
GS_MUTEX_UNLOCK(trackLock);
}

isExiting = NO;
}

Expand Down Expand Up @@ -431,18 +465,6 @@ + (BOOL) shouldCleanUp



struct trackLink {
struct trackLink *next;
id object; // Instance or Class being tracked.
IMP dealloc; // Original -dealloc implementation
IMP release; // Original -release implementation
IMP retain; // Original -retain implementation
BOOL global; // If all instance are tracked.
};

static struct trackLink *tracked = 0;
static gs_mutex_t trackLock = GS_MUTEX_INIT_STATIC;

static inline struct trackLink *
find(id o)
{
Expand Down Expand Up @@ -614,6 +636,8 @@ - (id) _replacementRetain

l = (struct trackLink*)malloc(sizeof(struct trackLink));
l->object = c;
l->instance = NO;
l->global = NO;
l->dealloc = class_getMethodImplementation(c, @selector(dealloc));
class_replaceMethod(c, @selector(dealloc),
method_getImplementation(replacementDealloc),
Expand Down Expand Up @@ -702,6 +726,7 @@ - (void) trackOwnership
*/
li = (struct trackLink*)malloc(sizeof(struct trackLink));
li->object = self;
li->instance = YES;
li->global = NO;
li->dealloc = lc->dealloc;
li->release = lc->release;
Expand Down

0 comments on commit e6b425d

Please sign in to comment.