EXC_BAD_ACCESS with NSDate

Discussion in 'iOS Programming' started by ipadzero, Oct 25, 2011.

  1. ipadzero macrumors newbie

    Joined:
    Oct 23, 2011
    #1
    Hi, I'm doing an application on my Ipad, I'm using NSDate to calculate time interval between 2 touches as below :

    in MyFile.h
    Code:
    @interface MyFile: CCLayer {
         NSDate* startTime;
    {
    @end
    
    in MyFile.m
    Code:
    -(id) init 
    {
        .........
        startTime = [NSDate date];  //initialize
        ..........
    }
    
    
    -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    {
            NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
            startTime= [NSDate date];
            if(timeInterval..........)
                     return FALSE;
            
    }
    

    Program received EXC_BAD_ACCESS at:
    NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];

    I'm not sure what I'm doing wrong here. Thanks for help.
     
  2. GoCubsGo macrumors Nehalem

    GoCubsGo

    Joined:
    Feb 19, 2005
    #2
  3. Menge macrumors 6502a

    Menge

    Joined:
    Dec 22, 2008
    Location:
    Amsterdam
    #3
    startDate = [NSDate date] is an autoreleased object which means it will be destroyed at the end of the init cycle. You might want to [ retain] it.
     
  4. ipadzero thread starter macrumors newbie

    Joined:
    Oct 23, 2011
    #4
    Thanks , it worked :)
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Since you retained it, did you do the other thing you need to do?
    Do you know what other thing you need to do?
    If not, did you read the memory management guidelines?
    If not, will you drop everything and read them?
     

Share This Page