Start stuff for handling cleanup on process exit.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33344 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2011-06-19 09:26:03 +00:00
parent db2099074d
commit 9831216043
12 changed files with 246 additions and 9 deletions

View file

@ -23,7 +23,9 @@
*/
#import "common.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSException.h"
#import "Foundation/NSLock.h"
#import "GNUstepBase/NSObject+GNUstepBase.h"
#import "GNUstepBase/NSDebug+GNUstepBase.h"
@ -125,3 +127,92 @@
@end
static NSMutableArray *leaked = nil;
static NSLock *leakLock = nil;
static BOOL shouldCleanUp = NO;
static void
handleExit()
{
int classCount;
if (YES == shouldCleanUp)
{
DESTROY(leaked);
DESTROY(leakLock);
}
classCount = objc_getClassList(NULL, 0);
if (classCount > 0)
{
Class *classes;
int index;
classes = malloc(sizeof(Class) * classCount);
classCount = objc_getClassList(classes, classCount);
for (index = 0; index < classCount; index++)
{
Class c = classes[index];
Method m = class_getClassMethod(c, @selector(atExit));
if (m != 0)
{
Class s = class_getSuperclass(c);
if (0 == s || class_getClassMethod(s, @selector(atExit)) != m)
{
[c atExit];
}
}
}
free(classes);
}
}
@implementation NSObject(atExit)
+ (void) enableAtExit
{
if (nil == leakLock)
{
leakLock = [NSLock new];
atexit(handleExit);
}
}
+ (void) atExit
{
return;
}
+ (id) leak: (id)anObject
{
[leakLock lock];
if (nil == leaked)
{
leaked = [NSMutableArray new];
}
[leaked addObject: anObject];
[leakLock unlock];
return anObject;
}
+ (void) setShouldCleanUp: (BOOL)aFlag
{
if (YES == aFlag)
{
[self enableAtExit];
shouldCleanUp = YES;
}
else
{
shouldCleanUp = NO;
}
}
+ (BOOL) shouldCleanUp
{
return shouldCleanUp;
}
@end