2013-10-29 35 views
0

在我的代碼:崩潰後分配到的UIImage UIImageView.image

* .h文件中

UIImageView* imgFACE; 
UIImage*  imgF; 
UIImage*  imgG; 

初始化:

imgF = [UIImage imageNamed: @"f.png"]; 
imgG = [UIImage imageNamed: @"g.png"]; 

UIImageView *img; 
img = [[UIImageView alloc]initWithFrame:FACE_RECT]; 
[self addSubview:img]; 
self.imgFACE = img; 
[img release]; 
中的drawRect

,頻頻:

NSLog(@"[%@]", imgF); 
if(something) self.imgFACE.image = imgF; 
else   self.imgFACE.image = imgG; 

NSLog的標準結果是:

[<UIImage: 0x5db6b0>] 

有時這只是代碼生成崩潰,NSLog的結果很奇怪:

[Length 4 (2 blocks, 1 used, block 0 is at 0) 4 0x146d22c0 
{ NSColor = "UIDeviceWhiteColorSpace 0 1"; 
    NSFont = "<UICTFont: 0x1454fc00> font-family: \".HelveticaNeueInterface-M3\"; 
    font-weight: normal; font-style: normal; font-size: 12.00pt"; 
    NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, 
    ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, 
    LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n 28L,\n 56L, 
    \n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), 
    DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, 
    HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0"; 
    NSShadow = "NSShadow {0, -1} color = {(null)}"; } 

什麼是類型的對象?

崩潰日誌:

0libobjc.A.dylib 0x3b393b26 objc_msgSend + 5 
1NOM2 0x000ad7a5 -[PanelInfoView myTimerMethod] (PanelInfoView.m:527) + 309157 
2Foundation 0x319a3ecd __NSFireTimer + 64 
3CoreFoundation 0x30f8b0e7 <redacted> + 14 
4CoreFoundation 0x30f8acff <redacted> + 782 
5CoreFoundation 0x30f8909b <redacted> + 1210 
6CoreFoundation 0x30ef3ce7 CFRunLoopRunSpecific + 522 
7CoreFoundation 0x30ef3acb CFRunLoopRunInMode + 106 
8GraphicsServices 0x35c14283 GSEventRunModal + 138 
9UIKit 0x33795a41 UIApplicationMain + 1136 
10NOM2 0x0006e44d main (main.m:25) + 50253 

因爲我認爲這是內存泄漏的結果。 012gimgF & imgG只初始化一次。

這個UIImage會發生什麼?

+0

對於公約(和另一點)使用點符號,而不是' - >' –

+2

@NekakKinich其實使用' - >'在這裏是正確的。這直接訪問伊娃。由於沒有屬性,使用'self.imageFACE'將會不正確。在Objective-C中通過'self->訪問ivars不是必需的。這是正確的,但沒有必要。 – rmaddy

+1

@skippy如果您要發佈關於崩潰的問題,如果您告訴我們完整的錯誤消息是什麼並提供有關錯誤來自何處的詳細信息,那麼它確實有幫助。 – rmaddy

回答

1
imgF = [UIImage imageNamed: @"f.png"]; 
imgG = [UIImage imageNamed: @"g.png"]; 

這兩者都是自動釋放的對象,

if(something) self.imgFACE.image = imgF; 
else   self.imgFACE.image = imgG; 

,如果事情爲否,則imgG變成被imgFACE如果你是幸運的,它是不是已經發布了保留,但imgF仍然不保留任何人,所以在下一個drawRect它是非常確定,它會崩潰在NSLog(@「[%@]」,imgF)或只是打印其他對象,如果它幸運地分配在該地址。

用途:

imgF = [[UIImage imageNamed: @"f.png"] retain]; 
imgG = [[UIImage imageNamed: @"g.png"] retain]; 

而在dealloc中釋放出來:

- (void)dealloc { 
    [imgF release]; 
    [imgG release]; 

    // cleanup other resources too 

    [super dealloc]; 
} 
+0

thx @imihaly,我必須測試它。代碼很大。這種情況不會發生在所有設備上;從未使用A4處理器,頻繁使用多核處理器。 – theWalker