NSThreads
NSThreads
- Subject: NSThreads
- From: Thierry Passeron <email@hidden>
- Date: Wed, 7 May 2003 15:16:42 +0200
Hello people,
i'm having a hard time trying to figure out what's going on in this
little program of mine, which is supposed to time my eggs-cooking stuff
:)
It's a simple timer program... you enter the amount of seconds or
minutes or hours you want, and it starts showing a progress bar and the
time elaepsed. At the end ... it makes a pretty sound :)
I made a simple controller class and some outlet to IB ...ok no problem
... and a loop method which is the method that is launched in an other
thread to increment the progress bar and the time, and within this
method there is not much code ... no Object retains ... and it leaks a
looooooot of memory (about 2MB/s).
here is it:
-(void)loop
{
[ startDate retain ];
[ endDate retain ];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if( running ){
while( ![[[ NSDate date ] earlierDate: endDate ] isEqual: endDate ] &&
running ){
//[ self increment ]; // the method that increment the progress
bar ...
}
(...) // some stuff
}
[ pool release ];
[ startDate release ];
[ endDate release ];
}
Also here is how i call it in the method "go" which is triggered by the
"GO!" button of the main window's interface.
- (IBAction)go:(id)sender
{
unsigned int i=0;
(...)
if( !running ){
running = TRUE;
(...)
i = [ inbox intValue ]; // IBOutlet inbox
startDate = [ NSDate date ];
endDate = [ NSDate dateWithTimeIntervalSinceNow: i ];
(...)
[ NSThread detachNewThreadSelector: @selector( loop )
toTarget: self
withObject: nil ];
}else{
running = FALSE;
(...)
}
(...)
}
I think the increment method is of no importance because without it,
the application's memory leaks even more (the loop goes even faster).
if you put a sleep(1); in the while loop it leaks less.
"running" is init'ed to FALSE in -(id)init;
also if needed:
@interface MyController : NSObject
{
(...)
NSDate * startDate;
NSDate * endDate;
BOOL running;
}
-(id)init;
- (IBAction)go:(id)sender;
@end
why does it leak so much memory? I know i use 2 NSDate objects in the
while test but it's never re-used... i mean even if i make the while
look like this :
while( running ){
NSDate * currentD = [ NSDate date ]; // retainCount = 1
NSDate * earlierD = [ currentD earlierDate: endDate ]; //
retainCount = 1
if( [ earlierD isEqual: startDate ] ){
running = FALSE;
}else
[ self increment ];
[ currentD release ]; // retainCount = 0
[ earlierD release ]; // retainCount = 0
}
It's leaking memory again ...
should i dealloc directly the pointers ? i tried ... SIGBUS one time
... same leaking and no SIGBUS the other time (unpredictable).
Anyone can help ?
Best regards,
Thierry.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.