gpt4 book ai didi

ios - 当 init 方法/参数包含单词 "delegate"时可能出现 XCode Analyzer 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:16 24 4
gpt4 key购买 nike

我在 XCode 5.1.1 中并且没有使用 ARC。

当我将单词 delegate 添加到 init 方法的任何参数的末尾时,如果您在不释放它的情况下分配它,XCode 的分析器不会提示。

下面的例子展示了这种行为。如果您将 initWithDelegate 更改为 initWithDelegat(末尾没有 e),则 Analyze 会按预期运行。

@interface mrTest : NSObject
-(id)initWithDelegate:(id)delegate;
@end

@implementation mrTest
-(id)initWithDelegate:(id)delegate {
self = [super init];
return self;
}
@end

-(void)example {
mrTest * t = [[mrTest alloc] initWithDelegate:nil];

// (just calling a method here so there are no build warnings)
[t toggleItalics:nil];

// Should complain that it's not being released, also doesn't complain if you uncomment this line
//[t release];
}

这不像是在以某种方式添加 autorelease,因为当您取消注释 [t release]; 行时,它会报错。

它甚至可以让你像这样过度释放它......

-(void)example {
mrTest * t = [[[[mrTest alloc] initWithDelegate:nil] autorelease] autorelease];

[t release];
[t release];
[t release];
[t release];
}

有兴趣知道是否有人有解释或以前经历过这种情况?

最佳答案

这似乎是 Clang 静态分析器有意做出的决定,从中可以看出源代码

http://clang.llvm.org/doxygen/RetainCountChecker_8cpp_source.html

01424   // If one of the arguments in the selector has the keyword 'delegate' we
01425 // should stop tracking the reference count for the receiver. This is
01426 // because the reference count is quite possibly handled by a delegate
01427 // method.

历史上也有解释

https://llvm.org/viewvc/llvm-project?view=revision&revision=158532

[analyzer] RetainCount: don't track objects init'd with a delegate

We already didn't track objects that have delegates or callbacks or objects that are passed through void * "context pointers". It's a not-uncommon pattern to release the object in its callback, and so the leak message we give is not very helpful.

关于ios - 当 init 方法/参数包含单词 "delegate"时可能出现 XCode Analyzer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com