gpt4 book ai didi

iphone - NSOperation 和 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-03 21:07:57 27 4
gpt4 key购买 nike

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

  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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com