私は iphone 開発にまったく慣れていないので、アプリケーションで奇妙なクラッシュが発生しています。実際、メモリ警告をシミュレートした後、私のアプリケーションは常にクラッシュします。私は毎回この動作を再現することができ、障害のある行を分離することができました:)。
カスタムUITableViewCellsを提供するカスタムUITableViewControllerで作業しています。
@implementation CustomTableViewController
// [...]
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyTableViewCell";
UITableViewCell *cell = nil;
if ([indexPath row] < [dataList childCount])
{
cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell)
{
cell = [[[KpowUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
KUICustomView* customView = [[KUICustomView alloc]initWithFrame:CGRectZero];
[(KpowUITableViewCell*)cell setFrontView:customView];
[customView release];
}
KUICustomView* cView = [(KpowUITableViewCell*)cell frontView];
[cView setDataObject:[dataList getChildAtIndex:[indexPath row]]]; // The crash happens in this function
}
// [...]
セルビューのカスタムデータオブジェクトを設定する関数は次のとおりです。
-(void)setDataObject:(DataObject *)do
{
[do retain];
[dataObject release];
dataObject = do;
NSString* defaultPath = [NSString stringWithFormat:@"%@/default_image.png", [[NSBundle mainBundle] resourcePath]];
UIImage* defaultImage = [[UIImage alloc] initWithContentsOfFile:defaultPath];
[self setImage: defaultImage];//[UIImage imageNamed:@"default_image"]]; // The crash happens in this function
[defaultImage release];
// [...]
そして最後に、ここで魔法が起こります:
-(void)setImage:(UIImage *)img
{
[img retain];
NSLog(@"setImage : old image > %@/%@/%i", [image description], [[image class]description], [image retainCount]);
[image release]; // CRASH EXC_BAD_ACCESS
image = img;
[self setNeedsDisplay];
}
したがって、通常のシナリオではすべてが正常に機能します。しかし、メモリ警告をシミュレートし、UITableView をスクロールすると、これらすべての関数が呼び出され、アプリケーションがクラッシュします。[image release] を削除すると、クラッシュは発生しません (ただし、「はい、メモリ リークが発生します」)。NSLog の出力は常に次のようになります。
setImage : old image > <UIImage: 0x4b54910>/UIImage/1
何が間違っているのか、この問題を回避するために何ができるのか、本当にわかりません。Xcodeデバッガーのスクリーンショットは次のとおりです...
http://img30.imageshack.us/i/debuggerscreen.png/
どんな助けでも大歓迎です。前もって感謝します
編集 1: @bbum Build and Analyze は、無関係な警告をいくつか表示しましたが、それでも有用です。それがそこにあるのさえ見ませんでした
画像を設定した場所がもう 1 つあります。ではsetDataObject
、イメージは単なるプレースホルダです。実画像のダウンロードを非同期で起動し、 に戻しrequestDidFinishLoad
ます。メソッドは次のようになります。
- (void)requestDidFinishLoad:(KURLRequest*)request
{
if (request == currentRequest)
{
UIImage* img = [[UIImage alloc] initWithData:[request data]];
if (nil != img)
[self setImage:img];
[img release];
}
if (currentRequest == request)
currentRequest = nil;
[request release];
}
NSZombie Detection で計測器を実行しましたが、結果は別の方向を示しているようです。スクリーンショットは次のとおりです。
http://img13.imageshack.us/i/zombieinstrument.jpg/
それをどうするかはまだよくわかりませんが、調査は進んでいます:)