2012-01-16 43 views
0

在我的應用程序中,我有一個循環,在UIImage陣列上移動,並使這個圖像的東西。 在後臺線程循環工作,以便在函數的開始,我把:[游泳池釋放];崩潰我的應用程序

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

,並最終

[pool release]; 

在循環中我創建UIImage,所以我需要釋放它,因爲它給我一個記憶警告,如果我沒有發佈。

當應用程序完成的循環,並獲得了

[pool release]; 

它給我BAD_ACCESS錯誤和崩潰的應用程序。

編輯


這是循環

 UIImage *tmp = [image rotate:UIImageOrientationRight]; 
     //do some stuff with this image 
     [tmp release]; 

這是rotate方法的方法:

UIImage*   copy = nil; 
    CGRect    bnds = CGRectZero; 
    UIImage*   copy = nil; 
    CGContextRef  ctxt = nil; 
    CGImageRef   imag = self.CGImage; 
    CGRect    rect = CGRectZero; 
    CGAffineTransform tran = CGAffineTransformIdentity; 

    rect.size.width = CGImageGetWidth(imag); 
    rect.size.height = CGImageGetHeight(imag); 

    bnds = rect; 

    UIGraphicsBeginImageContext(bnds.size); 
    ctxt = UIGraphicsGetCurrentContext(); 

switch (orient) 
{ 
    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     CGContextScaleCTM(ctxt, -1.0, 1.0); 
     CGContextTranslateCTM(ctxt, -rect.size.height, 0.0); 
     break; 

    default: 
     CGContextScaleCTM(ctxt, 1.0, -1.0); 
     CGContextTranslateCTM(ctxt, 0.0, -rect.size.height); 
     break; 
} 

CGContextConcatCTM(ctxt, tran); 
CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, imag); 

copy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

if (imag) { 
    CFRelease(imag); 
} 

return copy; 
+3

你可以發表代碼如何初始化你的UIImage? –

+0

我編輯我的文章的代碼 – MTA

+0

是的,當你耗盡從池創建以來已經被自動釋放的所有內容的池。這可能會導致兩個問題:1)釋放已釋放的內容 - 通常會導致出現此錯誤消息。 2)發佈真正需要的東西,比如你的一個UI對象。這通常會導致一個非常無用的崩潰消息。有時候,具體原因相當明顯,其他時候確實是一個挑戰。 –

回答

4

你旋轉之後在釋放你的形象。

UIImage *tmp = [image rotate:UIImageOrientationRight]; 
    //do some stuff with this image 
    [tmp release]; // Here 

UIGraphicsGetImageFromCurrentImageContext()返回一個自動釋放的對象,所以你不需要調用釋放它返回後。

當釋放NSAutoreleasePool時會發生崩潰,因爲最後一個-release不會被髮送,直到它被排空並向您的對象發送正確的釋放調用,該對象之前已被您錯誤釋放。

+0

打我吧。他的問題聽起來像一個過度釋放的對象,代碼證明了它。 –

0

我覺得你的崩潰,可能與時自動釋放池釋放UIImages,而不是釋放自動釋放池。

+0

有一種方法可以解決它? – MTA

+0

是的,刪除手動[釋放](或CFRelease())調用,讓自動釋放池照顧它 – ACBurk

1

也許你正在發佈一些你創建的對象,並且在你創建這個池的時候再次釋放它。

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

NSString *s = [NSString stringWithFormat:@"%d", 2]; 
// Your string now has a retain count of one, but it's autoreleased. So when the pool 
// gets released it'll release the string 

[s release]; 
// You decrease the retain count to zero, so the object gets destroyed 
// s now points to a deallocated object 

[pool release]; 
// The pool gets destroyed, so it tries to send a release method to your string. However, 
// the string doesn't exist anymore so an error occurs. 
+0

但如果我不釋放對象我的應用程序得到內存警告 – MTA

+0

這是正確的第一行,例子是不。首先,絕對保留數是無用的;別想到他們。其次,'NSString'是一個靜態分配的對象;保留/釋放是沒有任何操作的。 – bbum

+0

爲什麼絕對保留的想法無用?修正靜態分配的對象thingy – v1Axvw

相關問題