单身潜在泄漏?

Sim*_*iwi 1 xcode singleton memory-leaks analyzer ios

在我的项目中的第三方库中,单例sharedInstance方法似乎在最终的return_sharedInstance行中抛出了一个分析器警告:

+ (BlockBackground*)sharedInstance
{
    if (_sharedInstance != nil) {
        return _sharedInstance;
    }

    @synchronized(self) {
        if (_sharedInstance == nil) {
            [[[self alloc] init] autorelease];
        }
    }

    return _sharedInstance;
}
Run Code Online (Sandbox Code Playgroud)

无论如何,实际修复此警告的正确方法是什么?另外我已经看到你不应该在这样的方法中做自我分配,这是真的吗?

谢谢!

Yam*_*man 5

实际上你的线[[[self alloc] init] autorelease];在对象上创建并立即扔掉它.你需要替换_sharedInstance = [[self alloc] init];