2009-09-29 11 views
1

我正在追查一個奇怪的錯誤。在控制檯日誌中顯示此消息時會出現該錯誤:奇怪的malloc錯誤與指定大小的自動發佈的UIImages

BlurApp(5018,0xa00d6500) malloc: *** error for object 0x103b000: pointer being freed was not allocated 

此消息在執行離開我的事件循環後彈出。它似乎在autorelease池被耗盡時發生。下面是這似乎觸發了錯誤代碼:

- (UIImage*)imageWithImage:(UIImage*)image 
    scaledToSize:(CGSize)newSize; 
{ 
UIGraphicsBeginImageContext(newSize); 
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return newImage; 
} 

我打電話imageWithImage這樣:

UIImage* theImage = [self imageWithImage:origImage scaledToSize:rect.size]; 

theImage在調用程序使用,那麼我想自動釋放會照顧它。

如果我將return newImage;更改爲[newImage retain];,則看不到錯誤消息。

現在,這裏是很奇怪的。這個錯誤僅在特定大小的newSize時纔會發生。具體而言,當newSize的高度和寬度等於55到176範圍內的任何數字時,總是會發生這種情況,不包括56,110,112,114,而不是其他高度和寬度在5和300之間的值。(高度和寬度均爲總是等於所有測試)

我可以使用一些新鮮的眼睛在這...謝謝!

+0

這似乎是在模擬器3.0 SDK中的錯誤。我沒有看到這些模擬器的SDK 3.1的錯誤消息,我沒有看到iPhone上的錯誤。 – mahboudz 2009-09-30 03:22:57

回答

0

嘗試打印從UIGraphicsGetImageFromCurrentImageContext返回的圖像的retainCount。如下所示:NSLog(@「Image retention count:%u」,[newImage retainCount]);

或將斷點上的代碼和鍵入在gdb的控制檯以下:

打印(INT)[newImage retainCount]

的retainCount應大於零更大,可能之一。遍歷代碼並不斷檢查retainCount以查看它何時達到零。

有在類似的線程: iPhone development: pointer being freed was not allocated

+0

底部的鏈接指向一個類似的問題,這使得我認爲這是模擬器中的一個錯誤。我應該提到我在模擬器上運行它。我在iPhone上嘗試了這個應用程序。那裏沒有錯誤。我也嘗試過使用Simulator 3.1 SDK而不是3.0。在Simulator 3.1下沒有錯誤。 – mahboudz 2009-09-30 03:21:41

0

theImage is used in the calling routine and then I assume autorelease will take care of it.

If I change the return newImage; to return [newImage retain]; the error message is not seen.

Now, here is what is very strange. This error only occurs with certain sizes of newSize.

在垃圾收集語言,對象可以被垃圾收集被清理(釋放)隨時沒有該對象不再引用。爲了提高性能,垃圾收集可能會延遲 - 即直到有空閒時間,經過一段時間,或直到內存條件達到某個閾值。因此,一些對象可能會立即釋放,其他對象可能會在很久以後釋放,或者永遠不會釋放(直到環境關閉)。

+0

這是針對沒有垃圾收集的iPhone。 – 2009-09-29 21:44:56