Fix thread memory leak.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@17336 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2003-07-25 09:27:44 +00:00
parent e8fb1797f9
commit 778c3a3c7c
3 changed files with 152 additions and 122 deletions

View file

@ -4,6 +4,10 @@
change ... seemed to cause problems on some systems. change ... seemed to cause problems on some systems.
* Tools/gdnc.m: ditto. * Tools/gdnc.m: ditto.
* Source/NSDebug.m: Small thread safely fix. * Source/NSDebug.m: Small thread safely fix.
* Source/NSThread.m: Avoid multiple copies of housekeeping timer.
* Source/NSConnection.m: Fix thread related memory leak leaving
an NSRunLoop in a connection when the connection is no longer
using it.
2003-07-23 Richard Frith-Macdonald <rfm@gnu.org> 2003-07-23 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -2633,16 +2633,33 @@ static void callEncoder (DOContext *ctxt)
NSTimeInterval delay_interval = last_interval; NSTimeInterval delay_interval = last_interval;
NSDate *delay_date = nil; NSDate *delay_date = nil;
NSRunLoop *runLoop = [runLoopClass currentRunLoop]; NSRunLoop *runLoop = [runLoopClass currentRunLoop];
BOOL isLocked = NO;
BOOL addedRunLoop = NO;
/*
* If we have sent out a request on a run loop that we don't already
* know about, it must be on a new thread - so if we have multipleThreads
* enabled, we must add the run loop of the new thread so that we can
* get the reply in this thread.
*/
if (_multipleThreads == YES
&& [_runLoops indexOfObjectIdenticalTo: runLoop] == NSNotFound)
{
[self addRunLoop: runLoop];
addedRunLoop = YES;
}
NS_DURING
{
if (debug_connection > 5) if (debug_connection > 5)
NSLog(@"Waiting for reply sequence %d on %x:%x", NSLog(@"Waiting for reply sequence %d on %x:%x",
sn, self, [NSThread currentThread]); sn, self, [NSThread currentThread]);
M_LOCK(_queueGate); M_LOCK(_queueGate); isLocked = YES;
while (_isValid == YES while (_isValid == YES
&& (node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn)) != 0 && (node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn)) != 0
&& node->value.obj == dummyObject) && node->value.obj == dummyObject)
{ {
M_UNLOCK(_queueGate); M_UNLOCK(_queueGate); isLocked = NO;
if (timeout_date == nil) if (timeout_date == nil)
{ {
timeout_date = [dateClass allocWithZone: NSDefaultMallocZone()]; timeout_date = [dateClass allocWithZone: NSDefaultMallocZone()];
@ -2698,7 +2715,7 @@ static void callEncoder (DOContext *ctxt)
{ {
if (limit_date == timeout_date) if (limit_date == timeout_date)
{ {
M_LOCK(_queueGate); M_LOCK(_queueGate); isLocked = YES;
node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn); node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn);
break; break;
} }
@ -2707,17 +2724,17 @@ static void callEncoder (DOContext *ctxt)
else else
{ {
/* /*
* Normal operation - wait for data to be received or for a timeout. * Normal operation - wait for data or for a timeout.
*/ */
if ([runLoop runMode: NSConnectionReplyMode if ([runLoop runMode: NSConnectionReplyMode
beforeDate: timeout_date] == NO) beforeDate: timeout_date] == NO)
{ {
M_LOCK(_queueGate); M_LOCK(_queueGate); isLocked = YES;
node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn); node = GSIMapNodeForKey(_replyMap, (GSIMapKey)sn);
break; break;
} }
} }
M_LOCK(_queueGate); M_LOCK(_queueGate); isLocked = YES;
} }
if (node == 0) if (node == 0)
{ {
@ -2728,7 +2745,7 @@ static void callEncoder (DOContext *ctxt)
rmc = node->value.obj; rmc = node->value.obj;
GSIMapRemoveKey(_replyMap, (GSIMapKey)sn); GSIMapRemoveKey(_replyMap, (GSIMapKey)sn);
} }
M_UNLOCK(_queueGate); M_UNLOCK(_queueGate); isLocked = NO;
TEST_RELEASE(delay_date); TEST_RELEASE(delay_date);
TEST_RELEASE(timeout_date); TEST_RELEASE(timeout_date);
if (rmc == nil) if (rmc == nil)
@ -2749,6 +2766,27 @@ static void callEncoder (DOContext *ctxt)
format: @"invalidated while awaiting reply"]; format: @"invalidated while awaiting reply"];
} }
} }
if (addedRunLoop == YES)
{
addedRunLoop = NO;
[self removeRunLoop: runLoop];
}
}
NS_HANDLER
{
if (addedRunLoop == YES)
{
addedRunLoop = NO;
[self removeRunLoop: runLoop];
}
if (isLocked == YES)
{
M_UNLOCK(_queueGate);
}
[localException raise];
}
NS_ENDHANDLER
NSDebugMLLog(@"NSConnection", @"Consuming reply RMC %d on %x", sn, self); NSDebugMLLog(@"NSConnection", @"Consuming reply RMC %d on %x", sn, self);
return rmc; return rmc;
} }
@ -2951,21 +2989,6 @@ static void callEncoder (DOContext *ctxt)
reserved: [_sendPort reservedSpaceLength]]; reserved: [_sendPort reservedSpaceLength]];
M_LOCK(_refGate); M_LOCK(_refGate);
/*
* If we have sent out a request on a run loop that we don't already
* know about, it must be on a new thread - so if we have multipleThreads
* enabled, we must add the run loop of the new thread so that we can
* get the reply in this thread.
*/
if (_multipleThreads == YES && needsReply == YES)
{
NSRunLoop *loop = [runLoopClass currentRunLoop];
if ([_runLoops indexOfObjectIdenticalTo: loop] == NSNotFound)
{
[self addRunLoop: loop];
}
}
/* /*
* We replace the code we have just used in the cache, and tell it not to * We replace the code we have just used in the cache, and tell it not to

View file

@ -325,8 +325,9 @@ GSRunLoopForThread(NSThread *t)
r = [NSRunLoop new]; r = [NSRunLoop new];
[d setObject: r forKey: key]; [d setObject: r forKey: key];
RELEASE(r); RELEASE(r);
if (t == nil || t == defaultThread) if (housekeeper == nil && (t == nil || t == defaultThread))
{ {
CREATE_AUTORELEASE_POOL (arp);
NSNotificationCenter *ctr; NSNotificationCenter *ctr;
NSNotification *not; NSNotification *not;
NSInvocation *inv; NSInvocation *inv;
@ -334,7 +335,7 @@ GSRunLoopForThread(NSThread *t)
ctr = [NSNotificationCenter defaultCenter]; ctr = [NSNotificationCenter defaultCenter];
not = [NSNotification notificationWithName: @"GSHousekeeping" not = [NSNotification notificationWithName: @"GSHousekeeping"
object: r object: nil
userInfo: nil]; userInfo: nil];
sel = @selector(postNotification:); sel = @selector(postNotification:);
inv = [NSInvocation invocationWithMethodSignature: inv = [NSInvocation invocationWithMethodSignature:
@ -344,10 +345,14 @@ GSRunLoopForThread(NSThread *t)
[inv setArgument: &not atIndex: 2]; [inv setArgument: &not atIndex: 2];
[inv retainArguments]; [inv retainArguments];
housekeeper = [NSTimer timerWithTimeInterval: 30.0 housekeeper = [[NSTimer alloc] initWithFireDate: nil
invocation: inv interval: 30.0
target: inv
selector: NULL
userInfo: nil
repeats: YES]; repeats: YES];
[r addTimer: housekeeper forMode: NSDefaultRunLoopMode]; [r addTimer: housekeeper forMode: NSDefaultRunLoopMode];
RELEASE(arp);
} }
} }
} }
@ -408,23 +413,17 @@ gnustep_base_thread_callback()
*/ */
+ (NSThread*) currentThread + (NSThread*) currentThread
{ {
NSThread *t; NSThread *t = nil;
if (entered_multi_threaded_state == NO) if (entered_multi_threaded_state == NO)
{ {
/* /*
* The NSThread class has been initialized - so we will have a default * The NSThread class has been initialized - so we will have a default
* thread set up. * thread set up unless the default thread subsequently exited.
*/ */
t = defaultThread; t = defaultThread;
}
if (t == nil) if (t == nil)
{
fprintf(stderr, "ALERT ... [NSThread +currentThread] ... the "
"default thread is nil!");
fflush(stderr); // Needed for windoze
}
}
else
{ {
t = (NSThread*)objc_thread_get_data(); t = (NSThread*)objc_thread_get_data();
if (t == nil) if (t == nil)
@ -630,6 +629,10 @@ gnustep_base_thread_callback()
[NSAutoreleasePool _endThread: self]; [NSAutoreleasePool _endThread: self];
} }
} }
if (self == defaultThread)
{
defaultThread = nil;
}
NSDeallocateObject(self); NSDeallocateObject(self);
} }