iOS 5 の iPad アプリケーションで奇妙なエラーが発生するのに 1 日を費やしましたが、誰か情報を持っていないかと考えていました。
_currentModel
一般的なセットアップは次のとおりです。との 2 つのメンバー変数を持つ UIViewController サブクラス StoryViewChildController があります_comingModel
。StoryViewChildController には、[[INEWSStoryModel alloc] init]
作成された結果のオブジェクトを呼び出して返すメソッドがあります。の間viewDidLoad
に、次のコード ブロックがあります。これはコードから逐語的にコピーされたものではないことに注意してください-それはより複雑です-しかし、私は要約しようとしています:
_currentModel = [self createModel];
_comingModel = [self createModel];
その後、ある時点で、_currentModel と _comingModel を他のオブジェクトと交換できるようにする必要があります。私のコードからのそのままのスワップメソッドは次のとおりです。
- (void)swapComingPageAndCurrentPage {
[_swapLock lock];
//Swap story views
IPCStoryView *swapPage = _currentPage;
_currentPage = _comingPage;
_comingPage = swapPage;
//Swap models
INEWSStoryModel *swapModel = _currentModel;
_currentModel = _comingModel;
_comingModel = swapModel;
//Swap players
PlayerController *swapPlayer = _currentPlayerController;
_currentPlayerController = _comingPlayerController;
_comingPlayerController = swapPlayer;
// clear out the content of the old view
[_comingPage resetStoryView];
[_comingPlayerController resetPlayer];
_comingPlayerController.view.alpha = 0.0;
_currentPageURI = _lastRequestedStory;
[_swapLock unlock];
}
問題は、リリース構成 (コンパイラの最適化に -Os を使用) でプロジェクトを実行すると、モデル オブジェクトのスワップ中にクラッシュすることです。クラッシュは、Zombie オブジェクトにアクセスしようとしたことが原因です。Instruments を使用してオブジェクトの保持/解放パスを追跡したところ、このメソッドに入ると、予想どおり _currentModel の参照カウントが 1 になりました。ただし、この行INEWSStoryModel *swapModel = _currentModel;
では _currentModel で保持が呼び出されることはないため、次の呼び出し_currentModel = _comingModel;
で参照カウントが 0 にswapModel
落ちます。その後、範囲外になると、別の解放呼び出しが試行され、プログラムがクラッシュします。
コンパイラの最適化は、最適化すべきではない保持呼び出しを最適化しているように思えます。他の誰かがこの種の問題を抱えていますか? 他に何か間違ったことをしている可能性がありますか?必要に応じて、クラスからさらにコードを投稿できます。
その他の興味深い点: swapModel 変数を __autoreleasing に設定すると、コードは機能します。また、swap メソッドの最後に次のコード ブロックを追加すると、コードが機能します。
_comingPage.cueView.frame = [_comingPage adjustCueViewFrame:_comingPage.cueView.frame
ForVideoShowing: NO
inOrientation:_currentOrientation];
そのメソッドが行うのは、UIView フレームを調整することだけです。関係のないコードをメソッドの最後に追加してゾンビを作成しないという事実は、コンパイラが正しく最適化していないと私に思わせます。誰にもアイデアはありますか?