Fix memory leak.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25779 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2007-12-24 17:31:52 +00:00
parent 70ddc19001
commit 52f2105eb1
2 changed files with 59 additions and 32 deletions

View file

@ -3,6 +3,8 @@
* Source/NSRunLoop.m: Perhaps a bit paranoid, but alter to recheck * Source/NSRunLoop.m: Perhaps a bit paranoid, but alter to recheck
all timers after any timer is fired, so we ar sure to pick up any all timers after any timer is fired, so we ar sure to pick up any
changes done to timer fire dates during the firing of a timer. changes done to timer fire dates during the firing of a timer.
* Source/NSThread.m: Fix memory leak and correct thread startup
semantics to be like MacOS-X I hope.
2007-12-22 Richard Frith-Macdonald <rfm@gnu.org> 2007-12-22 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -538,11 +538,6 @@ gnustep_base_thread_callback(void)
{ {
NSThread *thread; NSThread *thread;
/*
* Make sure the notification is posted BEFORE the new thread starts.
*/
gnustep_base_thread_callback();
/* /*
* Create the new thread. * Create the new thread.
*/ */
@ -551,14 +546,8 @@ gnustep_base_thread_callback(void)
selector: aSelector selector: aSelector
object: anArgument]; object: anArgument];
/* [thread start];
* Have the runtime detach the thread RELEASE(thread);
*/
if (objc_thread_detach(@selector(start), thread, nil) == NULL)
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to detach thread (unknown error)"];
}
} }
@ -803,7 +792,42 @@ gnustep_base_thread_callback(void)
- (void) main - (void) main
{ {
if (_active == NO)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-$@] called on inactive thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
if (objc_thread_get_data() != nil)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-$@] called on running thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
/*
* We are running in the new thread - so we store ourself in the thread
* dictionary and release ourself - thus, when the thread exits, we will
* be deallocated cleanly.
*/
objc_thread_set_data(self);
/*
* Let observers know a new thread is starting.
*/
if (nc == nil)
{
nc = RETAIN([NSNotificationCenter defaultCenter]);
}
[nc postNotificationName: NSThreadDidStartNotification
object: self
userInfo: nil];
[_target performSelector: _selector withObject: _arg]; [_target performSelector: _selector withObject: _arg];
[NSThread exit];
} }
- (NSString*) name - (NSString*) name
@ -829,27 +853,28 @@ gnustep_base_thread_callback(void)
- (void) start - (void) start
{ {
/* if (_active == NO)
* We are running in the new thread - so we store ourself in the thread
* dictionary and release ourself - thus, when the thread exits, we will
* be deallocated cleanly.
*/
objc_thread_set_data(self);
_active = YES;
/*
* Let observers know a new thread is starting.
*/
if (nc == nil)
{ {
nc = RETAIN([NSNotificationCenter defaultCenter]); /* Make sure the notification is posted BEFORE the new thread starts.
} */
[nc postNotificationName: NSThreadDidStartNotification gnustep_base_thread_callback();
object: self
userInfo: nil];
[self main]; /* The thread must persist until if finishes executing.
[NSThread exit]; */
RETAIN(self);
/* Mark the thread as active whiul it's running.
*/
_active = YES;
if (objc_thread_detach(@selector(main), self, nil) == NULL)
{
_active = NO;
RELEASE(self);
[NSException raise: NSInternalInconsistencyException
format: @"Unable to detach thread (unknown error)"];
}
}
} }
/** /**