• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Unwanted retain
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unwanted retain


  • Subject: Re: Unwanted retain
  • From: Finlay Dobbie <email@hidden>
  • Date: Wed, 4 Feb 2004 23:21:52 +0000

On 4 Feb 2004, at 22:41, Lorenzo wrote:

I noted that when I call scheduledTimerWithTimeInterval, my self object
retainCounter increments by one.

Obviously, as the NSTimer you created needs your object to stick around until it's done with it.

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?

What you say doesn't really make much sense, but I'm guessing you mean you release your object when you're done with it. Did you invalidate the timer, though? Once the timer is invalidated, it too will release the object since it doesn't need it to stick around any more.

I just verified that there isn't a bug with NSTimer in this regard:

--

#import <Foundation/Foundation.h>

@interface Foo:NSObject
@end

@implementation Foo
- init {
[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(timerFire:) userInfo:nil repeats:NO];
return self;
}


- (void)timerFire:(NSTimer *)timer {
NSLog(@"timer fired");
}

- (void)dealloc {
NSLog(@"deallocated");
}

@end

int main (int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Foo *foo = [[Foo alloc] init];
[[NSRunLoop currentRunLoop] run];
[foo release];
[pool release];
return 0;
}

--

gonzales:~ finlayd$ ./foo
2004-02-04 23:20:47.456 foo[1472] timer fired
2004-02-04 23:20:47.456 foo[1472] deallocated

--

Since your timer repeats, you'll need to send it a -invalidate when you're done with it to make it go away.

HTH.

-- Finlay
_______________________________________________
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.

References: 
 >Unwanted retain (From: Lorenzo <email@hidden>)

  • Prev by Date: RE: Unwanted retain
  • Next by Date: Re: Unwanted retain
  • Previous by thread: RE: Unwanted retain
  • Next by thread: Re: Unwanted retain
  • Index(es):
    • Date
    • Thread