CGColorReleaseing strokeColour Causes Crash Before Release Line
I have a little method that does some CoreGraphics drawing. Today I was using Clang in XCode to detect and fix some memory leaks. It told me about 'rgbColourSpace' and 'fillColour' (see code below). I was wondering why it did not alert me to 'strokeColour' as I had not released that either. So I added the block at the end of the method, that releases all three. Strangely enough, my iPad app would crash in the line indicated below, which is actually above the line I release the 'strokeColour' in.
// 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 = CGColo开发者_如何学运维rSpaceCreateDeviceRGB();
// 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.
This puzzles me, but it's quite repeatable. If I comment the last line out, everything works; with the line activated it crashes a few lines above (line indicated above). What's going on? Is the drawing not occurring when I think it is? Is it all happening later? And if so, why do 'fillColour' and 'rgbColourSpace' not have that problem? Any hints would be greatly appreciated. Thank you.
You're not owner of fillColour
and strokeColour
. Do not release them.
精彩评论