アプリケーションに大きな問題があり、それをどのように解決するのか疑問に思っています。SOでたくさん検索しましたが、有効な解決策が見つかりません。これが私が取り組んでいるシナリオです。
私はNON-ARCアプリケーションを持っていて、その中でたくさんのARCクラスを使用しています。これらのクラスはGMGridViewに属しています。これらのクラスは、-fobjc-arcディレクティブを使用してプロジェクトに追加されています。
これは私が使用しているコードです(簡単にするために、重要な部分のみを追加しました)。
メモリ管理セクション
- (void)dealloc
{
[gmGridView setActionDelegate:nil];
[gmGridView setDataSource:nil];
[gmGridView release];
[super dealloc];
}
ViewDidLoadセクション
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger topBottomSpacing = 20;
NSInteger leftRifghtSpacing = 75;
NSInteger itemSpacing = 5;
UIView* mainView = [self view];
GMGridView* gridView = [[[GMGridView alloc] initWithFrame:mainView.bounds] autorelease];
gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
gridView.backgroundColor = [UIColor clearColor];
gridView.style = GMGridViewStyleSwap;
gridView.itemSpacing = itemSpacing;
gridView.minEdgeInsets = UIEdgeInsetsMake(topBottomSpacing, leftRifghtSpacing, topBottomSpacing, leftRifghtSpacing);
gridView.centerGrid = NO;
gridView.actionDelegate = self;
gridView.dataSource = self;
[mainView addSubview:gridView];
[self setGmGridView:gridView]; // retain policy
}
データソースセクション
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell) {
cell = [[[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
InternalView *view = [[[InternalView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
cell.contentView = view;
}
return cell;
}
ゾンビオブジェクトを使用している場合、アプリケーションは正常に機能します。エラーはありません。しかし、ゾンビオブジェクトを無効にすると、アプリケーションはメソッドでEXC_BAD_ACCESSmainをクラッシュさせます。ゾンビが有効になっていると、で発生するエラーの詳細が表示されるため、これは私にとって非常に奇妙ですmain。
よくわからないのautoreleaseはコード内の呼び出しですが、オブジェクトを自動解放プールに入れないとリークすると思います。
GMGridView* gridView = [[[GMGridView alloc] initWithFrame:mainView.bounds] autorelease];
cell = [[[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
[gmGridView release]少し調べてみると、deallocメソッドでコメントするとアプリがクラッシュしなくなることがわかりました。では、これはどういう意味ですか?電話しないとrelease漏れgmGridViewますか?
何か提案はありますか?前もって感謝します。
編集
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)indexメソッドにコードを追加しました。初めて追加するのを忘れました。
(タイプの)のdealloc方法が問題の根本にあるようです。ここにコードがあります。InternalViewUIView
- (void)dealloc
{
[addButton release]; // it's added to self addSubview, it has also a retain policy
[imageView release]; // it's added to detView addSubview, it has also a retain policy
[detView release]; // it's added to self addSubview, it has also a retain policy
[super dealloc];
}
コメント[detView release]すると、クラッシュはなくなります。