iphone - NSOperation 和 EXC_BAD_ACCESS

标签 iphone objective-c nsoperation

我有一些主要是数据驱动的应用程序,因此大多数屏幕基本上由以下部分组成:

  1. 打开屏幕
  2. 通过 NSOperation 下载数据
  3. 在 UITableView 中显示数据
  4. 从 UITableView 中进行选择
  5. 转到新屏幕,然后从第 1 步开始

我发现一切正常使用,但如果用户离开应用程序一段时间然后回来,我会在下一个 NSOperation 运行时收到 EXC_BAD_ACCESS 错误。如果用户是否将应用程序发送到后台,这似乎并不重要,并且只有在自上次数据连接建立起至少几分钟后才会发生这种情况。

我意识到这一定是某种形式的过度释放,但我的内存管理非常好,我看不出有什么问题。我的数据调用通常如下所示:

-(void)viewDidLoad {
    [super viewDidLoad];

    NSOperationQueue* tmpQueue = [[NSOperationQueue alloc] init];
    self.queue = tmpQueue;
    [tmpQueue release];
}

-(void)loadHistory {
    GetHistoryOperation* operation = [[GetHistoryOperation alloc] init];
    [operation addObserver:self forKeyPath:@"isFinished" options:0 context:NULL];
    [self.queue addOperation:operation];
    [operation release];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqual:@"isFinished"] && [object isKindOfClass:[GetHistoryOperation class]]) {
        GetHistoryOperation* operation = (GetHistoryOperation*)object;
        if(operation.success) {
            [self performSelectorOnMainThread:@selector(loadHistorySuceeded:) withObject:operation waitUntilDone:YES];
        } else {
            [self performSelectorOnMainThread:@selector(loadHistoryFailed:) withObject:operation waitUntilDone:YES];
        }       
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

-(void)loadHistorySuceeded:(GetHistoryOperation*)operation {
    if([operation.historyItems count] > 0) {
        //display data here
    } else {
        //display no data alert
    }
}

-(void)loadHistoryFailed:(GetHistoryOperation*)operation {
    //show failure alert 
}

我的操作通常如下所示:

-(void)main {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSError* error = nil;
    NSString* postData = [self postData];
    NSDictionary *dictionary = [RequestHelper performPostRequest:kGetUserWalkHistoryUrl:postData:&error];

    if(dictionary) {
        NSNumber* isValid = [dictionary objectForKey:@"IsValid"];
        if([isValid boolValue]) {
            NSMutableArray* tmpDays = [[NSMutableArray alloc] init];
            NSMutableDictionary* tmpWalksDictionary = [[NSMutableDictionary alloc] init];
            NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyyMMdd"];

            NSArray* walksArray = [dictionary objectForKey:@"WalkHistories"];
            for(NSDictionary* walkDictionary in walksArray) {
                Walk* walk = [[Walk alloc] init];
                walk.name = [walkDictionary objectForKey:@"WalkName"];
                NSNumber* seconds = [walkDictionary objectForKey:@"TimeTaken"];
                walk.seconds = [seconds longLongValue];

                NSString* dateStart = [walkDictionary objectForKey:@"DateStart"];
                NSString* dateEnd = [walkDictionary objectForKey:@"DateEnd"];
                walk.startDate = [JSONHelper convertJSONDate:dateStart];
                walk.endDate = [JSONHelper convertJSONDate:dateEnd];

                NSString* dayKey = [dateFormatter stringFromDate:walk.startDate];
                NSMutableArray* dayWalks = [tmpWalksDictionary objectForKey:dayKey];
                if(!dayWalks) {
                    [tmpDays addObject:dayKey];
                    NSMutableArray* dayArray = [[NSMutableArray alloc] init];
                    [tmpWalksDictionary setObject:dayArray forKey:dayKey];
                    [dayArray release];
                    dayWalks = [tmpWalksDictionary objectForKey:dayKey];
                }
                [dayWalks addObject:walk];
                [walk release];
            }

            for(NSString* dayKey in tmpDays) {
                NSMutableArray* dayArray = [tmpWalksDictionary objectForKey:dayKey];

                NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];
                NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
                NSArray* sortedDayArray = [dayArray sortedArrayUsingDescriptors:sortDescriptors];
                [sortDescriptor release];

                [tmpWalksDictionary setObject:sortedDayArray forKey:dayKey];
            }

            NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
            self.days = [tmpDays sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            self.walks = [NSDictionary dictionaryWithDictionary:tmpWalksDictionary];
            [tmpDays release];
            [tmpWalksDictionary release];
            [dateFormatter release];
            self.success = YES;
        } else {
            self.success = NO;
            self.errorString = [dictionary objectForKey:@"Error"];
        }
        if([dictionary objectForKey:@"Key"]) {
            self.key = [dictionary objectForKey:@"Key"];
        }
    } else {
        self.errorString = [error localizedDescription];
        if(!self.errorString) {
            self.errorString = @"Unknown Error";
        }
        self.success = NO;
    }

    [pool release];
}

-(NSString*)postData {
    NSMutableString* postData = [[[NSMutableString alloc] init] autorelease];

    [postData appendFormat:@"%@=%@", @"LoginKey", self.key];

    return [NSString stringWithString:postData];
}

----
@implementation RequestHelper

+(NSDictionary*)performPostRequest:(NSString*)urlString:(NSString*)postData:(NSError**)error {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kHostName, urlString]];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
    [urlRequest setHTTPMethod:@"POST"];
    if(postData && ![postData isEqualToString:@""]) {
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        [urlRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
        [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    }

    NSURLResponse *response = nil;  
    error = nil;
    NSData *jsonData = [NSURLConnection sendSynchronousRequest:(NSURLRequest *)urlRequest returningResponse:(NSURLResponse **)&response error:(NSError **)&error];

    NSString *jsonString = [[NSString alloc] initWithBytes: [jsonData bytes] length:[jsonData length]  encoding:NSUTF8StringEncoding];
    NSLog(@"JSON: %@",jsonString);

    //parse JSON
    NSDictionary *dictionary = nil;
    if([jsonData length] > 0) {
        dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:error];
    }

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    return dictionary;
}

如果我有自动释放池,崩溃会发生在[pool release]上。如果我不这样做,那么崩溃看起来只是出现在 main.m 方法中,而我似乎没有得到任何有用的信息。当我每次测试之间都必须等待 10 分钟时,很难追踪!

如果有人可以提供任何线索或路线,我们将不胜感激。

最佳答案

几乎可以肯定,您在代码中过度释放了某些内容,因为崩溃是在[池释放]期间发生的(主方法中也有一个自动释放池)。

您可以使用 Xcode 找到它 - 使用构建和分析让静态分析器查明潜在问题。运行它并发布结果。

关于iphone - NSOperation 和 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5307941/

相关文章:

ios - Apple Mach-O 链接器错误 - ld : file not found: -ObjC

iphone - 我如何在 xcode 4 中引用一个单独的项目?

objective-c-blocks - 如何取消 NSBlockOperation

objective-c - 我是否需要 Block_copy 传递给 NSOperation 的 setCompletionBlock :? 的 block

iphone - 我的 ARC 应用程序如何在 iOS 3.x 中运行?

iphone - MKMapView setRegion : odd behavior?

objective-c - UIButton "addTarget"崩溃应用程序

ios - 我可以使用一本关于 Objective-C 的旧书,它在今天仍然适用于构建 ios 7 应用程序吗?

iphone - ViewController 类型名称在一个类中无法识别,但在另一个类中却可以识别?

iOS:隐藏 UITableView 中特定部分的所有行