2012-06-14 42 views
4

我剛開始在覈心顯卡上發佈,所以我可能需要一些幫助。在覈心顯卡中發佈問題

我有代碼看起來像這樣:

UIImage *buttonImage() { 

UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 


    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); 


    CGMutablePathRef outerPath; 
    CGMutablePathRef midPath; 
    CGMutablePathRef innerPath; 
    CGMutablePathRef highlightPath; 

//Some button stuff 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    CGContextRelease(context); 

    return image; 

} 

該版本行了,我已經把雖然我得到一個錯誤吧。

context_reclaim: invalid context 
context_finalize: invalid context 

任何想法,以我應該在哪裏發佈這個版本?

回答

6

如果您之前完成了CFRetain(context)CGContextRetain(context),或者您自己創建了上下文,則只需執行CGContextRelease(context)。在你的例子中,你打電話給UIGraphicsBeginImageContextWithOptions(),這是處理你創建的上下文,因此,你自己調用CGContextRelease()過度釋放它。

你確實需要的CGColorSpaceCreateDeviceRGB()有兩種平衡:

CGColorSpaceRelease(baseSpace) 

或:

if (baseSpace) CFRelease(baseSpace) 
6

使用UIGraphicsGetCurrentContext時,你沒有自己的背景。所以你不應該釋放它。如果你要使用CGContextRetain()那麼你會釋放。這更多信息,請訪問:

Memory Management Guide for Core Foundation

我強烈建議您閱讀Memory Management節目指南得到它是如何工作的理解。