For some background, I have read the Docs multiple times but for some reason I still suck at trying to control my memory usage.
I am setting up an NSTimer to call a method every .2 seconds. In that method I check to see if the current time on the device has changed at all and if so I walk through each element of the time and check what has changed. It could be a second, minute or both etc...
The app runs for right about 10 minutes and crashes every time. I believe it is due to memory issues. Can anyone offer and advice on this code, or an alternate method for doing this? i believe the issues is with the variable current_time. I'm not sure exactly when to release it or re-create it.
Thanks
I am setting up an NSTimer to call a method every .2 seconds. In that method I check to see if the current time on the device has changed at all and if so I walk through each element of the time and check what has changed. It could be a second, minute or both etc...
The app runs for right about 10 minutes and crashes every time. I believe it is due to memory issues. Can anyone offer and advice on this code, or an alternate method for doing this? i believe the issues is with the variable current_time. I'm not sure exactly when to release it or re-create it.
Thanks
Code:
-(void)runTimer
{
myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.2
target: self
selector: @selector(showActivity)
userInfo: nil
repeats: YES];
}
-(void)showActivity
{
NSLog(@"showActivity");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// This will produce a time that looks like "12:15:00 PM".
[formatter setTimeStyle:NSDateFormatterMediumStyle];
tempTime = [[NSString alloc] initWithString:[formatter stringFromDate:[NSDate date]]];
[formatter release];
if([tempTime length] == 10)
{
NSString *newStr = [NSString stringWithFormat:@"0%@", tempTime];
tempTime = [NSString stringWithFormat:@"%@", newStr];
}
if(ftime==TRUE)
{
current_time = [[NSString alloc] init];
current_time = [[NSString alloc] initWithString:tempTime];
}
NSLog(@"TT[%@] CT[%@]", tempTime, current_time);
if (![current_time isEqualToString:tempTime] || ftime==TRUE)
{
int i=0;
for(i=0; i<[tempTime length]; i++)
{
if (([tempTime characterAtIndex:i] != [current_time characterAtIndex:i]) || ftime==TRUE)
{
.... call my method here if needed
}
}
[current_time release];
current_time = [[NSString alloc] initWithString:tempTime];
}
ftime=FALSE;
[tempTime release];
}