0

I have call a methode using timmer like this.

 timmer=    [NSTimer scheduledTimerWithTimeInterval:5.0
                                                   target:self
                                                 selector:@selector(callWaiter)
                                                 userInfo:nil repeats:YES];

here is the code of callWaiter

 -(void)callWaiter
{ 
@try

    {
        NSLog(@"current serverTime----1=%@",currentSeverTime);
        NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@table_update?tag=update&admin=%@&table=%@&time=%@",baseUrl,adminId,@"Waitercall",[currentSeverTime stringByReplacingOccurrencesOfString:@" " withString:@"%20"]]];
        JsonParse5 *js5=[[JsonParse5 alloc] init];
        if([[js5 fetchData:[NSData dataWithContentsOfURL:url]] count]>0)
        {
            UIAlertView *grAlert=[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@%@",LSSTRING(@"Waiter call - please attend table #"),[[[js5 fetchData:[NSData dataWithContentsOfURL:url]] objectAtIndex:0] objectForKey:@"table_no"] ] message:nil delegate:self cancelButtonTitle:LSSTRING(@"OK") otherButtonTitles:nil];

            [grAlert show];
            grAlert.tag=1;
            [grAlert release];


        }
        else
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            // this is imporant - we set our input date format to match our input string
            // if format doesn't match you'll get nil from your string, so be careful
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *dateFromString = [[NSDate alloc] init];
            // voila!
            dateFromString = [dateFormatter dateFromString:currentSeverTime];
            NSDate *correctDate = [NSDate dateWithTimeInterval:5.0 sinceDate:dateFromString];
            currentSeverTime=[dateFormatter stringFromDate:correctDate];

            NSLog(@"current serverTime----2=%@",currentSeverTime);






        }

    }
    @catch (NSException *exception) {
        NSLog(@"exception=%@",exception);
    }


}

it work fine first time while crashed on second time just before "current serverTime----1" log. I am assigning value in currentSeverTime as follows.

currentSeverTime=@"2013-07-16 07:15:50";

please help me thanks.

Jitendra
  • 5,055
  • 2
  • 22
  • 42
Suraj
  • 113
  • 2
  • 10

1 Answers1

0

I think the reason is that after

currentSeverTime=[dateFormatter stringFromDate:correctDate];

currentSeverTime is not retained, and currentSeverTime got released when reach the end of method. And currentSeverTime became a wild reference. It caused the EXC_BAD_ACCESS when use it second time.

You can make currentSeverTime as a property to correct the code. In .h file:

@property (retain) NSString *currentSeverTime;

In .m file:

  1. in dealloc method:

    [_currentSeverTime release];

  2. using it this way:

    self.currentSeverTime=[dateFormatter stringFromDate:correctDate];

Hope it help.

Andrew
  • 1,088
  • 10
  • 21