Kenny Wyland
Kenny Wyland

Reputation: 21870

Potential leak (when using garbage collection)

I'm running an Analyze on my Cocoa app with automatic garbage collection and receiving the following error message:

Potential leak (when using garbage collection) of an object allocated on line 1243

This is what is on line 1243:

self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);

Here is the definition of the self.positiveValueColor property:

@property (assign) CGColorRef positiveValueColor

The Analyzer is reporting the error later on though, down in the method below it. "double largestValue = 0.0" is where the error appears, even though it references line 1243:

Here is the entire method for reference:

- (void) setDefaultColors {
    if (self.positiveValueColor == nil) {
        self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);
    }

    if (self.negativeValueColor == nil) {
        self.negativeValueColor = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0);
    }

    if (self.zeroValueColor == nil) {
        self.zeroValueColor = CGColorGetConstantColor(kCGColorBlack);
    }

}

- (BOOL) largestValueIsPositive {
    double largestValue = 0.0;

    if (self.pv != nil) {
        double value = [self.pv doubleValue];
        if (fabs(value) > fabs(largestValue)) {
            largestValue = value;
        }
    } 
    ... // method continues on

Why am I getting that analyze error?

-- EDIT --

Thanks, Chuck! That worked. Here is what I replaced the relevant lines with:

self.positiveValueColor = (CGColorRef)CFMakeCollectable(CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0));

Upvotes: 1

Views: 264

Answers (1)

Chuck
Chuck

Reputation: 237060

CGColorRefs are not ordinarily eligible for garbage collection. You should use CFMakeCollectable(). That's what it's warning about.

Upvotes: 2

Related Questions