Re: [newbie] NSString code so it won't leak
Re: [newbie] NSString code so it won't leak
- Subject: Re: [newbie] NSString code so it won't leak
- From: Ondra Cada <email@hidden>
- Date: Thu, 12 Sep 2002 18:47:41 +0200
On Thursday, September 12, 2002, at 03:12 , Dean Davis wrote:
I even pointed out that I thought it may be something to do with NSTimer
but was told "Nope, wrong again."
Quite. Wrong again, indeed.
So it turns out that my example application will eat
memory unless it is interacted with. A timer firing by
itself won't trigger the code that releases (not free)
the standard AutoRelease pool.
So this would effect any background type application
that relies on NSTimers to run code?
Twice nope. I've cleaned up your code a bit to see what would happen with
just an NSTimer:
#import "Foo.h"
@interface Reporter:NSObject
+(Reporter*)newReporter;
@end
@implementation Reporter
static NSMutableSet *leaks=nil;
long total=0;
+(Reporter*)newReporter {
Reporter *myself=[[[self alloc] init] autorelease];
if (!leaks) leaks=[[NSMutableSet alloc] init];
[leaks addObject:[NSValue valueWithPointer:myself]];
NSLog(@"created %x",myself);
total++;
return myself;
}
-(void)dealloc {
NSValue *key=[NSValue valueWithPointer:self];
if ([leaks containsObject:key]) [leaks removeObject:key];
NSLog(@"freed %x",self);
[super dealloc];
}
@end
@implementation Foo
- (IBAction)pushbutton:(id)sender {
minuteTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(minuteTimerFired:) userInfo:nil repeats:YES];
[minuteTimer retain];
}
- (void)minuteTimerFired:(NSTimer *)aTimer {
[Reporter newReporter];
}
- (IBAction)stopbutton:(id)sender {
NSLog(@"Leaks %d of %d",[leaks count],total);
[minuteTimer invalidate];
[minuteTimer release];
}
@end
Not suprisingly, autoreleasing works as it should and as documented --
here's a sample output:
2002-09-12 18:11:34.552 LeakTest[14315] created 1c7e950
2002-09-12 18:11:34.552 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:35.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:35.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:36.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:36.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:37.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:37.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:38.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:38.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:39.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:39.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:40.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:40.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:41.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:41.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:42.551 LeakTest[14315] created 1c7e950
2002-09-12 18:11:42.551 LeakTest[14315] freed 1c7e950
2002-09-12 18:11:42.814 LeakTest[14315] Leaks 0 of 9
Just for kicks, I've looked who releases the objects; of course it is the
pool, as it should be:
#0 -[Reporter dealloc] (self=0x2079c30, _cmd=0x706ece90) at
Foo.m:18/private/tmp/LeakTest/
#1 0x708115c4 in -[NSObject release] ()
#2 0x708121f0 in NSPopAutoreleasePool ()
#3 0x708d0744 in __NSFireTimer ()
#4 0x70196984 in CFRunLoopRemoveTimer ()
#5 0x7017c17c in CFRunLoopRunInMode ()
#6 0x701b6ba0 in CFRunLoopRunSpecific ()
#7 0x7017b804 in CFRunLoopRunInMode ()
#8 0x7312d614 in InvokeThemeButtonDrawUPP ()
#9 0x7314056c in GetNextWindow ()
#10 0x73171340 in BlockUntilNextEventMatchingListInMode ()
#11 0x70bd70b8 in _DPSNextEvent ()
#12 0x70bfe5d8 in -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#13 0x70c23468 in -[NSApplication run] ()
#14 0x70c91ed0 in NSApplicationMain ()
#15 0x000038a0 in main (argc=1, argv=0xbffffdf0) at main.m:13/private/tmp/
LeakTest/
#16 0x000037c4 in _start ()
#17 0x000035f4 in start ()
---
Ondra Hada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.