Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress XCode Analyzer Potential leak of an object stored into a .. error [duplicate]

I have a method that creates an returns an instance of a CoreGraphics object - CGPathRef.

When I run the app through Analyzer it complains that this method is leaking...it is , but it is deliberate. I do want to transfer the ownership to the caller and let them clean up.

What can I do to suppress this Analyzer warning?

- (CGPathRef) createSomePath:(CGPoint)center innerRadius:(CGFloat)innerRadius outerRadius:(CGFloat)outerRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle
{
  CGMutablePathRef slicePath = CGPathCreateMutable();

  ....

  return slicePath;  <--- Analyzer points to this line as a potential leak.
}

Assume this should be possible as lots of frameworks return these objects requiring the caller to clean up...

Thanks in advance !

P.S. Am afraid that this question is not a dup nor does not have a proper answer anywhere else...the 3 answers highlighted at the top of this page are not proper/complete...only the answer provided here by Matthias Bauch i.e. "new" rule is truly the right answer to the question I raised :) THANKS!

like image 634
Moonwalker Avatar asked Dec 26 '22 01:12

Moonwalker


1 Answers

CoreFoundation functions follow the "create rule". If the function has a "Create" in it it returns an object with a reference count of 1.

Unfortunately your method is not a c function.

So you should turn your Objective-C method into a C function. Something like this:

CGPathRef CreatePath(CGPoint center, CGFloat innerRadius, CGFloat outerRadius, CGFloat startAngle, CGFloat endAngle) {
    // your code here
}

This should please the analyzer and work correctly with ARC.

In a Objective-C method you can get similar behaviour by starting your method signature with new, alloc, copy or mutableCopy. Starting the method with new probably fits your case best.
Something like this should please the analyzer, but I am not sure if it works correctly with ARC. You have to profile this.

- (CGPathRef)newPath:(CGPoint)center innerRadius:(CGFloat)innerRadius outerRadius:(CGFloat)outerRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {
    // your code here
}
like image 164
Matthias Bauch Avatar answered Feb 22 '23 23:02

Matthias Bauch