简体   繁体   中英

If statement causes a crash in the iOS device but not in the Simulator

So, this code runs properly, without causing any crashes in the iOS simulator. However, on my iOS device (my iPhone), this causes a crash! Can anyone guess why? The logs haven't helped anywhere.

NSInteger loops = 1;
char character = '.';
if ([futureTr rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound) {
    NSArray *dotsArray = [futureTr componentsSeparatedByString:[NSString stringWithFormat:@"%c",character]];
    loops = [dotsArray count];
    if ([[dotsArray objectAtIndex:loops-1] isEqualToString:@""] || [dotsArray objectAtIndex:loops-1] == nil) {
        loops--;
    }
}

And I know that it's this statement that causes trouble, because I comment it out and the app just works fine! What can be going on?

Based on the discussion in the comments, the problem is that futureTr is being released at some point and this code then attempts to use the now deallocated pointer resulting in the "BAD_ACCESS" exception.

Proper memory management of this variable will alleviate the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM