Re: Unwanted retain
Re: Unwanted retain
- Subject: Re: Unwanted retain
- From: "b.bum" <email@hidden>
- Date: Wed, 4 Feb 2004 15:32:02 -0800
On Feb 4, 2004, at 2:41 PM, Lorenzo wrote:
I noted that when I call scheduledTimerWithTimeInterval, my self object
retainCounter increments by one.
-------
NSLog(@"A self retain count %d", [self retainCount]);
renderTimer = [[NSTimer scheduledTimerWithTimeInterval:timeInterval
target:self selector:@selector(fireTheTimer)
userInfo:nil
repeats:YES] retain];
NSLog(@"B self retain count %d", [self retainCount]);
-------
I get:
-------
A self retain count 1
B self retain count 2
-------
Then if I deallocate one time only the object "self" from the class I
created it, I don't see the object self deallocated. I have to
deallocate it
twice, but then my app crashes.
Why? Do you know how to fix this?
Your app is crashing because you are effectively "stealing" the retain
owned by the timer. The timer doesn't know this and when the timer
fires, it is effectively trying to send -fireTheTimer to an object that
no longer exists. *BOOM* Program crashes.
The timer retains the object because the timer isn't done with the
object yet.
If you want the timer to -release the target, then tell the timer to
-invalidate and it will. This will also prevent the timer from trying
to message a dead object.
b.bum
_______________________________________________
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.