Re: Unwanted retain
Re: Unwanted retain
- Subject: Re: Unwanted retain
- From: Jens Bauer <email@hidden>
- Date: Thu, 5 Feb 2004 21:51:41 +0100
Hi Bill,
On Thursday, Feb 5, 2004, at 20:28 Europe/Copenhagen, b.bum wrote:
This horse deserves flogging as it is a very large source of confusion.
On Feb 5, 2004, at 10:14 AM, Jens Bauer wrote:
-I always released the timer after invalidating it.
I don't know whether this would make my application crash or not,
however, I experienced crashes now and then, while clicking the
close-button in a window, where I had a timer running!
If you called -retain (or +alloc), you should -release. If you
didn't, you should not. If you are relying on the NSRunLoop to
retain your timer, your code is buggy.
This makes it very clear. It sounds like I was doing the right thing. =)
-But I still have a "precision-question":
If I have a few methods like these:
- (void)timerTask:(NSTimer *)aTimer
{
[self setNeedsDisplay:YES];
}
- (void)timerStart
{
float frequency;
if(timer)
{
[self timerStop];
}
frequency = 1.0 / 75.0;
timer = [NSTimer scheduledTimerWithTimeInterval:frequency target:self
selector:@selector(timerTask:) userInfo:NULL repeats:YES];
// [timer retain];
}
- (void)timerStop
{
if(timer)
{
[timer invalidate];
// [timer release];
timer = NULL;
}
}
-Should I then retain/release the timer ?
...I believe I should.
My code usually retains/releases, but even though it does this, I
(sometimes) get crashes (when closing the window), when I place the
code in a NSDocument, and in my -windowWillClose delegate method have
the following code:
- (void)windowWillClose:(NSNotification *)aNotification
{
[self timerStop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
...I can't see what I'm doing wrong. I'm still puzzled about what could
go wrong in that code.
When I had a [self timerStop] in the -dealloc method, I had even more
crashes; this is most likely because of a race-condition.
Love,
Jens
_______________________________________________
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.