抑制存储在.. .中的对象的XCode分析器潜在泄漏。错误(复制)

时间:2022-02-02 19:38:56

This question already has an answer here:

这个问题已经有了答案:

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

我有一个方法可以创建一个返回CoreGraphics对象的实例——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.

当我通过Analyzer运行应用程序时,它会抱怨这个方法正在泄漏……是的,但这是故意的。我确实想把所有权转让给打电话的人,让他们清理。

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!

注:恐怕这个问题不是一个问题,也没有一个合适的答案……在页面顶部突出显示的3个答案是不正确的/完整的…只有Matthias Bauch给出的答案是。“新”规则确实是我提出的问题的正确答案:)谢谢!

2 个解决方案

#1


9  

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

CoreFoundation函数遵循“创建规则”。如果函数中有一个“Create”,则返回一个引用计数为1的对象。

Unfortunately your method is not a c function.

不幸的是,您的方法不是c函数。

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

所以你应该把Objective-C方法变成C函数。是这样的:

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.

在Objective-C方法中,可以通过使用新的、alloc、copy或mutableCopy启动方法签名来获得类似的行为。用新方法开始新方法可能最适合你的情况。类似这样的东西应该会让分析仪满意,但我不确定它是否能正确地使用ARC。你必须对它进行剖析。

- (CGPathRef)newPath:(CGPoint)center innerRadius:(CGFloat)innerRadius outerRadius:(CGFloat)outerRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {
    // your code here
}

#2


0  

You should be able to do this:

你应该能够做到:

#ifndef __clang_analyzer__
CGMutablePathRef slicePath = CGPathCreateMutable();
#endif

I can't say I recommend it though!

我不能说我推荐它!

#1


9  

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

CoreFoundation函数遵循“创建规则”。如果函数中有一个“Create”,则返回一个引用计数为1的对象。

Unfortunately your method is not a c function.

不幸的是,您的方法不是c函数。

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

所以你应该把Objective-C方法变成C函数。是这样的:

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.

在Objective-C方法中,可以通过使用新的、alloc、copy或mutableCopy启动方法签名来获得类似的行为。用新方法开始新方法可能最适合你的情况。类似这样的东西应该会让分析仪满意,但我不确定它是否能正确地使用ARC。你必须对它进行剖析。

- (CGPathRef)newPath:(CGPoint)center innerRadius:(CGFloat)innerRadius outerRadius:(CGFloat)outerRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {
    // your code here
}

#2


0  

You should be able to do this:

你应该能够做到:

#ifndef __clang_analyzer__
CGMutablePathRef slicePath = CGPathCreateMutable();
#endif

I can't say I recommend it though!

我不能说我推荐它!