Re: Unwanted retain
Re: Unwanted retain
- Subject: Re: Unwanted retain
- From: Jörn Salewski <email@hidden>
- Date: Thu, 05 Feb 2004 00:29:42 +0100
am 04.02.2004 23:41 Uhr schrieb Lorenzo unter email@hidden:
>
Hi,
>
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?
>
>
Best Regards
Never _deallocate_ an object, instead _release_ it!
Only send the amount of release-messages to an object, to balance the alloc,
copy or retain messages that _you_ send to it. Don't care about the actual
retain count.
NSTimer apparently claims his interest in the 'self' object. It does so by
sending a retain message.
It is the duty of that NSTimer to balance this retain with a release at an
appropriate time. Usually this will happen, when the timer instance is
deallocated, that is when he recieved his last release message himself.
You'll see, if you release the timer the retain count of 'self' should be 1
again (at least, if no other object has claimed an interest in the
meantime).
One usually does't [self release].
I'd recomend to read the documentation about memory management again.
Pls, take a look at the archives
http://cocoa.mamasam.com/
to find plenty of online documentation about that topic.
Cheers,
Joern Salewski
_______________________________________________
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.