PDA

View Full Version : NSTimer Frustration: Crashes on second call?




Soulstorm
Jul 21, 2011, 12:48 PM
I have a timer set up inside a singleton. I can't understand why it's keeping crashing the second time the repeating function gets called!

static SFSession *_sharedSFSession = nil;

@interface SFSession ()
@property (nonatomic, retain) NSTimer *keepAliveTimer;
- (void)initDefaults;
- (void)reloadKeepAliveTimer;
- (void)keepSessionAliveUsingTimer:(NSTimer *)timer;
@end

@implementation SFSession
@synthesize loginData = _loginData;
@synthesize keepAliveTimer = _keepAliveTimer;

#pragma mark - General Init;
+ (SFSession *)sharedSFSession
{
@synchronized(self){
if (_sharedSFSession == nil) {
self = [[self alloc]init];
}
}
return _sharedSFSession;
}

+(id)allocWithZone:(NSZone *)zone
{
@synchronized(self){
if (_sharedSFSession == nil) {
_sharedSFSession = [super allocWithZone:zone];
return _sharedSFSession;
}
}
return nil;
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (id)retain
{
return self;
}

- (unsigned)retainCount
{
return UINT_MAX;
}



- (id)autorelease
{
return self;
}

#pragma mark - Special Init
- (id) init
{
self = [super init];
if (self != nil) {
[self initDefaults];
}
return self;
}

-(void)initDefaults
{
_loginData = [[SFDreamLoginData alloc]init];
[self reloadKeepAliveTimer];
}

#pragma mark - Functions
- (void)keepSessionAliveUsingTimer:(NSTimer *)timer
{
NSLog(@"keeping session alive!");
// [_keepAliveTimer invalidate];

}

- (void)reloadKeepAliveTimer
{
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(keepSessionAliveUsingTimer:) userInfo:nil repeats:YES];
}
#pragma mark -

- (void)dealloc {
[_loginData release];
[_keepAliveTimer release];
[super dealloc];
}


Any help would be greatly appreciated. I am using Xcode 4.1.



chown33
Jul 21, 2011, 01:53 PM
What is the stack trace when it crashes?

BTW, your singleton code doesn't match this example:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html%23//apple_ref/doc/uid/TP40002974-CH4-SW31

Among other differences, you don't override -release.