2009-08-10 9 views
0

:私の前の質問、Can somebody give me a hand about this stacktrace in iPhone app?からUIImageが決して解放されないようにする方法は?私はiPhoneからクラッシュログつかん

Exception Type: EXC_BAD_ACCESS (SIGBUS) 
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c 
Crashed Thread: 0 

Thread 0 Crashed: 
0 libobjc.A.dylib     0x30011940 objc_msgSend + 20 
1 CoreFoundation     0x30235f1e CFRelease + 98 
2 UIKit       0x308f4974 -[UIImage dealloc] + 36 
3 CoreFoundation     0x30236b72 -[NSObject release] + 28 
4 UIKit       0x30a00298 FlushNamedImage + 64 
5 CoreFoundation     0x30250a20 CFDictionaryApplyFunction + 124 
6 UIKit       0x30a0019c _UISharedImageFlushAll + 196 
7 UIKit       0x30a00730 +[UIImage(UIImageInternal) _flushCacheOnMemoryWarning:] + 8 
8 Foundation      0x3054dc7a _nsnote_callback + 178 
9 CoreFoundation     0x3024ea52 _CFXNotificationPostNotification + 298 
10 Foundation      0x3054b854 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64 
11 Foundation      0x3054dbba -[NSNotificationCenter postNotificationName:object:] + 14 
12 UIKit       0x30a00708 -[UIApplication _performMemoryWarning] + 60 
13 UIKit       0x30a006a0 -[UIApplication _receivedMemoryNotification] + 128 
14 UIKit       0x30a005d0 _memoryStatusChanged + 56 
15 CoreFoundation     0x30217410 __CFNotificationCenterDarwinCallBack + 20 
16 CoreFoundation     0x3020d0aa __CFMachPortPerform + 72 
17 CoreFoundation     0x30254a70 CFRunLoopRunSpecific + 2296 
18 CoreFoundation     0x30254164 CFRunLoopRunInMode + 44 
19 GraphicsServices    0x3204529c GSEventRunModal + 188 
20 UIKit       0x308f0374 -[UIApplication _run] + 552 
21 UIKit       0x308eea8c UIApplicationMain + 960 
... 
... 

を、私は主にUIImageの一部の周りに私のコードを変更しました。私は今[[UIImage alloc] initWithContentsOfFile ...]を使います。これ以上の[UIImage imageNamed:...]などはありません。その部分は以下の通りです。

//this is a method of a subclass of UIImageView. 
    - (void) reviewImage: (bool) review{ 
     NSString* st; 
     if (review){ 
     NSString* origin = [NSString stringWithString: [[ReviewCardManager getInstance] getCardImageString:chosenIndex]]; 
     NSString* stt = [origin substringToIndex: [origin length]-4]; 

     st = [[NSString alloc] initWithString: stt]; 

     if (myImageFlipped == nil) 
     myImageFlipped = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:@"png"]]; 
     [self setImage:myImageFlipped]; 

     if (notRotated){ 
      self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]); 
      notRotated = false; 
     } 
    }else{ 
     st = [[NSString alloc] initWithFormat:@"sc%d", chosenNumber]; 

     if (myImage == nil) 
     myImage = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:@"png"]]; 

     [self setImage:myImage]; 

     if (notRotated){ 
      self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]); 
      notRotated = false; 
     } 

    } 
    [st release]; 
} 

また、プロパティに既にUIImageが保持されています。

@property (nonatomic, retain) UIImage* myImage, *myImageFlipped; 

メモリリークも考慮されています。これらの変数は、deallocメソッドで解放されます。

私はこのバグをうまく解消したと思っていましたが、まだまだ珍しいバグが発生しているようです。

クラッシュログに基づいて、私のアプリケーションは "performMemoryWarning"を叫びます。私はちょうどサイズ156 x 272の13の.pngイメージを "割り当て"ています。私は混乱しています。それらの画像は、それがiPhoneのRAMを超えるという点まであまりメモリを取るべきではありません。それとも、私は見落としているものがありますか?お知らせ下さい。

+0

あなたのプロパティは「myImage」と表示されますが、セッターは「setImage:」と呼ばれます。ここで何が起こっているのか説明していただけますか? – hatfinch

+0

私はUIImageViewをサブクラス化していますが、このクラスには2つのUIImageがあります。 UIImageViewの画像を変更したいときは、基本的にsetImageを呼び出します。 – Karl

答えて

0

問題は解決しました。私はUIImageを1か所で変更するのを忘れていました。さて、すべてのUIImageは本当に "alloc"です。それ以上の自動解放はありません。

[UIImage imageNamed:...]を使用している場合、実際のデバイスのメモリが不足しているときに問題があるかどうかを確認するには、「Simulate Memory Warning」を使用してください。

0

はドキュメントから、あなたはUIImageのimageNamed convienceメソッドを使用する場合があります、メモリの問題とUIImagesのお手伝いをするには、次の

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

また、あなたはまだ実行した場合this routeに行きたいかもしれませんUIImage imageNamedに切り替えた後は、メモリの問題になります。なぜなら、convinienceメソッドの使用にはいくつかの欠点があるからです。

+0

いいえ、ポスターはすでにimageNamedから* away *離れていますが、imageNamedのキャッシュはプログラマーの制御を超えているため、これらのメモリ警告に対処するために推奨していたと思います。 – hatfinch

+0

ああ、私はそのセクションを読んだ。その場合、リンクされたブログ記事のようなカスタムキャッシュがより理にかなっています。 –

関連する問題