2013-07-29 37 views
2

分析儀正在標記內存問題。通常我會使用autorelease,但在Core Foundation中這是不可能的。我如何解決這個錯誤?核心圖形內存泄漏

Screen shot of error

- (CGMutablePathRef)NewCGMutablePathRefCreateWithRoundedRectForRect:(CGRect)rect andRadius:(CGFloat)radius andMargin:(CGFloat)margin andrIndent:(CGFloat)rIndent andlIndent:(CGFloat)lIndent 
{ 
CGMutablePathRef path = CGPathCreateMutable(); 

CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect) + margin); 
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMinY(rect) + margin, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMaxY(rect) - margin, radius); 
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMaxY(rect) - margin, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMaxY(rect) - margin, radius); 
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMaxY(rect) - margin, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMinY(rect) + margin, radius); 
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMinY(rect) + margin, CGRectGetMaxX(rect) - margin, CGRectGetMinY(rect) +margin, radius); 
CGPathCloseSubpath(path); 


return path; 
} 

將釋放路徑代碼的建議,我得到另一個錯誤加上原來的一個後?

additional screen shot

回答

1

在外部創建路徑,並嘗試在該方法中僅傳遞此路徑的引用。然後,您可以在您調用方法的相同範圍內釋放路徑。內存泄漏問題將消失。

4
CFRelease(path); 

CoreFoundation reference

使用CFRelease你不需要路徑之後了。

CGMutablePathRef path = [obj NewCGMutablePathRefCreateWithRoundedRectForRect:rect andRadius:radius andMargin:margin andrIndent:rIndent andlIndent:lIndent]; 
//do something with path, then release it 
CFRelease(path); 

另一件事是返回未自動釋放對象的方法有開始:

  • 頁頭
  • 副本
  • 保留

所以你應該爲您的方法命名: newCGMutablePathRefCreateWithRoundedRectForRect 代替: NewCGMutablePathRefCreateWithRoundedRectForRect

+0

感謝您的答覆。我應該在什麼時候釋放它,如果我在返回之前執行它,它將返回NULL? – geminiCoder

+0

沒有你在其他一些點來釋放它,看到更新的答案 – peko

+0

我嘗試這一點,但遭到了其他錯誤再次看到更新的答案 – geminiCoder

0
CGPathCloseSubpath(path); 

添加該代碼,返回該變量path之前。

+0

它已經在那裏,見上面,因爲你的方法命名的 – geminiCoder

+0

哦。在這種情況下,你可以使用'CGPathRelease'。 –

+0

不會返回NULL嗎? – geminiCoder

0

完成使用後釋放路徑變量。例如:

在您的代碼中的其他一些方法中,您將使用此路徑rt?

CGMutablePathRef路徑= [OBJ NewCGMutablePathRefCreateWithRoundedRectForRect:RECT andRadius:半徑andMargin:餘量andrIndent:rIndent andlIndent:lIndent];

....... .......

CFRelease(路徑);

1

如果要在兩個簡單的步驟解決這個問題:

  1. 重命名方法從NewCGMutablePathRefCreateWithRoundedRectForRectCreateCGMutablePathRefWithRoundedRectForRect。重命名它的重要部分是該方法以Create開頭,該方法告訴靜態分析器,您的方法返回需要釋放的對象並停止您所看到的警告。
  2. 通過致電CGPathRelease發佈返回的CGPathRef