Search code examples
objective-cxcodeclang-static-analyzer

silence a compiler warning about releasing a CGMutablePathRef object


I have enabled the static analyzer, but it is telling me that at the end of that execution path that object didn't get released, hence possibly causing a memory leak. I am however passing that reference to the created object to another class which will release it. I was wondering if there is a method or keyword to tell the compiled that I will release this object later.

I am looking for something like the auto-release.

By the way, I am using ARC.

I create the object like this:

CGMutablePathRef pathRef = CGPathCreateMutable();

And pass it like this:

self.flowView.pathToDraw = pathRef;

In my flowView class I have this method that will release it.

-(void) setPathToDraw:(CGMutablePathRef) newPath {
    if(pathToDraw!=NULL) CGPathRelease(pathToDraw);
    pathToDraw=newPath;
    [self setNeedsDisplay];
}

I already tried looking at the GCPath documentation but I had no luck.

Thanks


Solution

  • Yes, there is an extension for that:

    http://clang.llvm.org/docs/LanguageExtensions.html#objc_features

    You may declare your method as:

    - (void)setPathToDraw:(CGMutablePathRef) __attribute__((cf_consumed)) newPath
    

    and then Clang will recognize this (from the callsite -- it fails to check that you do in fact consume it in the definition).

    You need to make sure that every selector which defines this adheres to the attribute you have applied for the selector (name).

    Attributes are risky - i recommend sticking to conventions where possible, and being extra cautious when dealing with dynamic dispatch. Here's an example using ARC where the compiler can get it wrong. If the compiler gets it wrong, then the chances are good you will too because you're working against the tools that are trying to help you.

    IIRC, consume is the only attribute I have used, and I use it exclusively with static dispatch.