NSTimer Frustration: Crashes on second call?

Discussion in 'iPhone/iPad Programming' started by Soulstorm, Jul 21, 2011.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    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!

    Code:
    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.
     
  2. chown33, Jul 21, 2011
    Last edited: Jul 21, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #2

Share This Page