Objective-C - 泄漏在哪里?

时间:2011-11-28 10:13:36

标签: objective-c memory-leaks instruments

仪器向我显示我在以下代码中有泄漏:

            CGContextMoveToPoint(c, startPoint.x, self.frame.size.height - offsetYBottom);
            CGContextAddLineToPoint(c, startPoint.x, startPoint.y);
            CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
            CGContextAddLineToPoint(c, endPoint.x, self.frame.size.height - offsetYBottom);
            CGContextClosePath(c);

            CGGradientRef myGradient;
            CGColorSpaceRef myColorspace;

            size_t num_locations = 2;
            CGFloat locations[2] = { 0.0, 1.0 };
            CGFloat components[8] = { 0.0/255.0, 197.0/255.0, 254.0/255.0, 1.0f, 0.0/255.0, 197.0/255.0, 254.0/255.0, 0.25f };

            myColorspace = CGColorSpaceCreateDeviceRGB();
            myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

            CGPoint myStartPoint, myEndPoint;
            myStartPoint.x = self.frame.size.width / 2;
            myStartPoint.y = 0.0;
            myEndPoint.x = self.frame.size.width / 2;
            myEndPoint.y = self.frame.size.height;

            CGContextSaveGState(c);
            CGContextClip(c);
            CGContextDrawLinearGradient (c, myGradient, myStartPoint, myEndPoint, 0);
            CGContextRestoreGState(c);

如果我评论这一部分,泄漏就会消失。 startPoint和endPoint是CGPoint。

负责的来电者:CGTypeCreateInstanceWithAllocator。

可能是什么问题?

2 个答案:

答案 0 :(得分:3)

Create Rule之后,您必须在完成后发布myColorspacemyGradient

CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);

答案 1 :(得分:1)

尝试释放myGradient对象

CGGradientRelease(myGradient);