Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I avoid this memory warning?

I have a method that returns a CGPath and is generating analyzer warnings. The method is declared in a protocol. Here is an example implementation that is generating the warning:

"Potential leak of an object allocated on line 47 and stored into 'path'":

- (CGPathRef)createPathForBounds:(CGRect)bounds key:(NSString *)key;
{
    if ([key isEqualToString:OvalColumn])
    {
        CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
        return path;
    }

    return NULL;
}

Here is example usage that is generating the warning, "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"

CGPathRef path = [self.delegate createPathForBounds:bounds key:someKey];

// Use the path to do some drawing

CGRelease(path);

My memory management is correct; I'm passing back a retained CGPath from my protocol method and I'm releasing it in the calling block, so I know the warnings can be ignored, but I'd like to remove them altogether.

Am I missing a naming convention that will make the analyzer happy? Can functions be defined in protocols? How will subclassing work?

like image 840
kubi Avatar asked Aug 25 '11 15:08

kubi


People also ask

What is memory warning?

onLowMemory() raises when the system global memory is low. If the global memory is okay but your process takes more than 24MB on old Android devices, it will crash and you'll never get a low memory warning.

How do I reduce memory usage on my IPAD?

Go to Settings > General and go through your apps that have the most data. If your streaming or other apps occupy a couple of gigs and you don't have anything downloaded on those services, uninstall the apps and reinstall them. This will clear out the cache.

How memory leak happens in IOS?

As per Apple, a memory leak is:Memory that was allocated at some point, but was never released and is no longer referenced by your app. Since there are no references to it, there's now no way to release it and the memory can't be used again.


1 Answers

- (CGPathRef)newPathForBounds:(CGRect)bounds key:(NSString *)key

a detailed note on the topic can be found here

alternatively, you could have chosen to use the attribute cf_returns_retained, but it's best (imo) to favor naming conventions.

like image 152
justin Avatar answered Oct 08 '22 23:10

justin