Re: Unwanted retain
Re: Unwanted retain
- Subject: Re: Unwanted retain
- From: Chris Parker <email@hidden>
- Date: Wed, 4 Feb 2004 15:26:06 -0800
Hi Lorenzo,
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?
The timer is trying to guarantee that the object receiving the message
will be around when it's time to fire - so it's placing a retain on the
target.
So you've got one retain from you yourself creating the object - that
one you should release.
When the timer goes away, it'll release your object.
As a general rule, you shouldn't be using -retainCount to decide
whether or not to release something.
.chris
--
Chris Parker
Cocoa Frameworks Engineer
Apple Computer, Inc.
_______________________________________________
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.