Disable compiler warning about CGMutablePathRef object release

I turned on the static analyzer, but it tells me that at the end of this execution path this object is not freed, which is possibly causing a memory leak. However, I pass this link to the created object to another class that will release it. I was wondering if there is a way or keyword to tell the compile that I will release this object later.

I am looking for something like an auto release.

By the way, I use ARC.

I create an object as follows:

CGMutablePathRef pathRef = CGPathCreateMutable(); 

And pass it like this:

 self.flowView.pathToDraw = pathRef; 

In my flowView class, I have this method that will issue 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

+4
source share
2 answers

Yes, there is an extension for this:

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

You can declare your method as:

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

and then Clang finds out (from callsite - he cannot verify that you really consume it in the definition).

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

Attributes are risky - I recommend sticking to conventions where possible and being careful when working with dynamic sending. Here is an example of using ARC, where the compiler might make a mistake . If the compiler is wrong, the chances are good, you too, because you work with tools that try to help you.

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

+2
source

Why don't you just follow a normal conservation / release agreement? I do not see what you hope to receive.

Another challenge to save and release will not have a noticeable difference in performance, and it will be much more understandable for anyone who ever needs to read this code.

 CGMutablePathRef pathRef = CGPathCreateMutable(); self.flowView.pathToDraw = pathRef; CGPathRelease(pathRef); -(void) setPathToDraw:(CGMutablePathRef) newPath { if (pathToDraw != newPath) { CGPathRelease(pathToDraw); pathToDraw=CGPathRetain(newPath); [self setNeedsDisplay]; } } 

If you insist on doing it in a weird way, another alternative is to use the cf_consumed attribute in the declaration . This explains to the analyzer that you are doing something unusual.

+1
source

Source: https://habr.com/ru/post/1402804/


All Articles