CristiC
CristiC

Reputation: 22698

Objective-C - Where is the leak here?

Instruments shows me that I have a leak in the following code:

            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);

If I comment this portion, the leaks are gone. startPoint and endPoint are CGPoint.

Responsible caller: CGTypeCreateInstanceWithAllocator.

What could be the problem?

Upvotes: 0

Views: 253

Answers (2)

beryllium
beryllium

Reputation: 29767

Try to release myGradient object

CGGradientRelease(myGradient);

Upvotes: 1

omz
omz

Reputation: 53551

Following the Create Rule, you have to release myColorspace and myGradient when you're done with them:

CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);

Upvotes: 3

Related Questions