NSURLConnectionのクラッシュ
-
22-07-2019 - |
質問
NSOperationを作成し、NSURLConnectionsを同期的に開くことにより、サーバーでのバックエンドデータの読み込みを実装しています。
このコードは、通知がメインスレッドに戻るプログレスバーを追加するまで、シミュレータとデバイスの両方で完全に機能していました。
- (void)start {
// stop execution if it's cancelled
if([self isCancelled]) {
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
}
// If the operation is not canceled, begin executing the task.
[self willChangeValueForKey:@"isExecuting"];
[NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
}
同期呼び出しでEXC_BAD_ACCESSがクラッシュするようになりました。
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/index.php", rootURL]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSError *error = nil;
NSURLResponse *response = nil;
receivedData = [[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error] mutableCopy];
スタックトレースは次のように実行されます:
#0 0x3430debc in objc_msgSend ()
#1 0x3136f996 in NSPopAutoreleasePool ()
#2 0x31374160 in -[NSAutoreleasePool release] ()
#3 0x331a0408 in _UIApplicationHandleEvent ()
#4 0x325339c4 in PurpleEventCallback ()
#5 0x3147a52a in CFRunLoopRunSpecific ()
#6 0x31479c1e in CFRunLoopRunInMode ()
#7 0x3314ec08 in -[UIApplication _run] ()
#8 0x3314d230 in UIApplicationMain ()
#9 0x00002ab0 in main (argc=1, argv=0x2ffff504)
NSZombieEnabledを使用すると、取得します
*** -[NSDateComponents release]: message sent to deallocated instance 0x172fd0
接続が確立されていることを知っています(サーバーログを見て、リクエストが通過しています)。 NSURLConnectionの前のNSOperationコードのどこにもNSDateComponentオブジェクトが割り当てられていません。
私はNSURLConnectionで突然間違ったことを解決して、嫌いになろうとしました。
ご協力ありがとうございます!
解決 2
この質問を開発者フォーラムにも投稿し、並行性を削除するよう提案しました。問題のある行を削除しました:
[NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];
startのオーバーライドも停止し、mainのみをオーバーライドしました。
NSURLConnectionはクラッシュを停止しました。
他のヒント
私はあなたの問題が何であるかを正確には知りませんが、NSURLConnectionを使用しているときにランダムなエラーやクラッシュが発生することを知っています。その後、 ASIHTTPRequest を見つけましたが、振り返ることはありません。 NSURLConnectionのドロップイン代替品であり、CFNetworkから実行されます。これは非常に安定しており(すべてのプロジェクトで使用しています)、組み込みの進行状況バーのサポートや部分的なダウンロードの再開などの便利な機能が付属しています。 iPhoneで重要なRAMに保存するためにディスクに直接書き込むこともできます。