使有关释放 CGMutablePathRef 对象的编译器警告静音 [英] silence a compiler warning about releasing a CGMutablePathRef object

查看:49
本文介绍了使有关释放 CGMutablePathRef 对象的编译器警告静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启用了静态分析器,但它告诉我在执行路径的末尾该对象没有被释放,因此可能导致内存泄漏.然而,我将对创建的对象的引用传递给另一个将释放它的类.想知道有没有方法或者关键字告诉编译后我会释放这个对象.

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.

顺便说一下,我正在使用 ARC.

By the way, I am using ARC.

我像这样创建对象:

CGMutablePathRef pathRef = CGPathCreateMutable();

然后像这样传递:

self.flowView.pathToDraw = pathRef;

在我的 flowView 类中,我有这个方法可以释放它.

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];
}

我已经尝试查看 GCPath 文档,但我没有运气.

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

谢谢

推荐答案

是的,有一个扩展:

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

您可以将您的方法声明为:

You may declare your method as:

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

然后 Clang 会识别出这一点(来自调用站点——它无法检查您是否确实在定义中使用了它).

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).

属性有风险 - 我建议尽可能遵守约定,并在处理动态调度时格外小心.这是一个使用 ARC 的示例,其中编译器可能会出错.如果编译器出错,那么您也很有可能出错,因为您正在与试图帮助您的工具作斗争.

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 是我唯一使用过的属性,并且我专门将它用于静态调度.

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

这篇关于使有关释放 CGMutablePathRef 对象的编译器警告静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆