繁体   English   中英

我该如何解决此内存泄漏? NSInvocation

[英]How can I solve this memory Leak? NSInvocation

-(void)invokeMethod
{
    NSMethodSignature * sig = [[source class] instanceMethodSignatureForSelector:@selector(mySelector:)];
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:sig];

    [invocation setTarget:myTarget];
    [invocation setSelector:@selector(mySelector:)];

    MySubClassOfNSInvocationOperation * command = [[[MySubClassOfNSInvocationOperation alloc] initWithInvocation:invocation] autorelease];

    //setArgument retains command
    [invocation setArgument:&command atIndex:2];

    //addOperation retains command until the selector is finished executing
    [operationQueue addOperation:command];
}


-(void)mySelector:(MySubClassOfNSInvocation*)command
{
    //Do stuff
}

我不知道到底发生了什么,但是NSInvocationMySubClassOfNSInvocationOperation正在泄漏

当我删除行时:

[invocation setArgument:&command atIndex:2];

它不会泄漏,因此将命令作为参数传递时会出现某种问题。

您可能有一个引用计数循环...一种情况,其中command保留invocationinvocation保留command并且都不希望在自己的dealloc方法之前释放-导致它们从未被释放。

您需要确定两个对象中哪个哪个对象在层次结构上优先于另一个对象,并确保该下级对象不保留另一个对象。 顺便说一句- NSInvocation ,除非你调用不会保留参数retainArguments 或者,您可以实现close方法,手动告诉一个释放另一个,从而中断循环。

在我自己的一个项目中发现NSInvocation确切问题后,我写了“ 避免保留周期的规则 ”一文。

似乎setArgument方法保留了缓冲区(在这种情况下-您的命令对象)。 设置后可以尝试释放命令。 但您应该小心)。 当我的朋友的应用程序无法在新的iPhone操作系统上运行时,我的朋友感到困惑,因为他通过添加一行附加附加的发布消息来纠正了苹果的漏洞。 当苹果在新操作系统中进行更正时,此行是导致应用崩溃的原因)

这行上的多余的&符号是什么:

[invocation setArgument:&command atIndex:2];

您正在传递命令的指针。 对我来说这似乎是错误的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM