CGColorReleaseing strokeColour在发布行之前导致崩溃

时间:2011-02-23 12:19:10

标签: ipad memory-management memory-leaks core-graphics

我有一个做一些CoreGraphics绘图的方法。今天我在XCode中使用Clang来检测和修复一些内存泄漏。它告诉我'rgbColourSpace'和'fillColour'(见下面的代码)。我想知道为什么它没有提醒我'strokeColour',因为我也没有释放它。所以我在方法结束时添加了块,释放了所有三个块。奇怪的是,我的iPad应用程序会在下面指示的行中崩溃,这实际上是我释放'strokeColour'的行。

// Retrieve the path for the supplied bedID
CGPathRef path = (CGPathRef)[self.myDictionary objectForKey:bedID];

// Save the graphics state so it can be restored later
CGContextSaveGState(self.bitmapContext);

// Initialise fill and stroke colour
CGColorRef fillColour = [UIColor whiteColor].CGColor;
CGColorRef strokeColour = [UIColor blackColor].CGColor;

// Set the stroke width
CGContextSetLineWidth(self.bitmapContext, 1.0);

// Create a CGColorSpace
CGColorSpaceRef rgbColourSpace = CGColorSpaceCreateDeviceRGB();

// Add the path to the graphics context
CGContextAddPath(self.bitmapContext, path);

// Set the fill colour...
CGContextSetFillColorWithColor(self.bitmapContext, fillColour);
// ...and stroke colour
--> CGContextSetStrokeColorWithColor(self.bitmapContext, strokeColour); // Crashes here

// Draw the path, with fill and stroke
CGContextDrawPath(self.bitmapContext, kCGPathFillStroke);

// Restore the graphics state
CGContextRestoreGState(self.bitmapContext);

// Notify the view to update itself
[self.view setNeedsDisplay];

// Release rgbColorSpace and fillColor
CGColorSpaceRelease(rgbColourSpace);
CGColorRelease(fillColour);
// CGColorRelease(*strokeColour*); -- for some reason we must not release this, or else the app crashes further up.

这让我感到困惑,但它是可重复的。如果我评论最后一行,一切正常;激活线后,它会在上面撞几行(上面的线条)。这是怎么回事?当我认为它没有发生时?这一切都发生在以后吗?如果是这样,为什么'fillColour'和'rgbColourSpace'没有那个问题?任何提示将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

您不是fillColourstrokeColour的所有者。不要释放它们。