Hey Devs,

I've always been having problems with Foundation's NSTimer Class when it comes to Memory-Management. After activating the static analyzer in Xcode (very useful!), it immediately reminds me of autoreleasing my instance of NSTimer and I thought like why is the NSTimer never being deallocated?? OKay so I created a demo NSTimer memory app and started testing with Instruments (Object Allocations Tool). In my opinion, there's a serious memory leak here since it's impossible to release an NSCFTimer. If I create it like that (should be actually autoreleased if repeat is set to NO and must be released manually or by calling - invalidate if repeat is set to YES):

[NSTimer scheduledTimerWithTimeInterval:0.1
                                                            target:self
                                                        
selector:@selector(fire:)
                                                        userInfo:nil
                                                        repeats:YES];

I sent an -invalidate to my instance of NSTimer in -fire: but Instruments told me the object was still alive. This is really confusing, I think the instance should be released when sending an invalidate so mem-management is clean and fine as it is in every other Apple ObjC class.

Since that didn't work I added a -release after invalidating the timer. This results in an EXC_BAD_ACESS (Invalid address) although its retain count was 1. I guess there's something else going wrong inside the NSTimer.

Unfortunately, the Apple NSTimer docs don't say anything about mem managment (nothing in discussion or sth like that)…

That's why I tried allocating the timer with alloc/init this way:

[[[NSTimer alloc] initWithFireDate:[NSDate date]
                                         interval:0.1
                                            target:self
                                        selector:@selector(fire:)
                                        userInfo:nil
                                         repeats:YES] autorelease];

After adding it to the current run-loop and invalidating it in -fire: it still remained in memory. Forcing an deallocation with an extra release (Note: this is just wrong since I never retained it without doing a release afterwards!) it crashed again with an invalid address.

What am I doing wrong? There really should be a way to deallocate an instance of NSTimer, I guess.

Thanks, guys!

Best regards,
Tobias Jordan._______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to