我应该如何避免这个内存警告?

发布于 2024-12-01 19:12:31 字数 705 浏览 0 评论 0原文

我有一个返回 CGPath 并生成分析器警告的方法。该方法在协议中声明。以下是生成警告的示例实现:

“第 47 行分配并存储到‘路径’中的对象存在潜在泄漏”:

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

    return NULL;
}

者此时不拥有的对象的引用计数的不正确递减”

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

// Use the path to do some drawing

CGRelease(path);

以下是生成警告的示例用法,“调用 内存管理正确;我从协议方法传回保留的 CGPath,并在调用块中释放它,因此我知道可以忽略警告,但我想将它们完全删除。

我是否缺少一个能让分析器满意的命名约定? 可以在协议中定义函数吗? 子类化将如何工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-12-08 19:12:31

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

有关该主题的详细注释可以在 此处

或者,您可以选择使用属性 cf_returns_retained,但最好(imo)支持命名惯例。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文