如何正确释放NSInvocation的保留参数?

时间:2011-06-23 05:51:41

标签: iphone nsinvocation

在释放NSInvocation时是否释放保留的参数,还是需要手动对NSInvocation的参数列表中的对象进行释放?

2 个答案:

答案 0 :(得分:6)

“保留的论点”? NSInvocation不会自动保留参数。参见:

This class does not retain the arguments for the contained invocation by default. If those 
objects might disappear between the time you create your instance of NSInvocation and the 
time you use it, you should explicitly retain the objects yourself or invoke the 
retainArguments method to have the invocation object retain them itself.

来源:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html

使用“retainArguments”时,您不必再次手动释放它们。 NSInvocation通过将它们添加到自动释放池来为您执行此操作。请参阅:http://www.cocoabuilder.com/archive/cocoa/241994-surprise-nsinvocation-retainarguments-also-autoreleases-them.html

答案 1 :(得分:3)

通过Google,我找到this conversation,这解释了为什么您不需要发布参数的关键原因:

  
    

所以我决定 - 保持评论,并假设这意味着我是     当我完成时应该释放目标和参数     它们。

  
     

正如你所发现的那样,不,你不是   应该这样做。你还没有   保留了目标和参数。   你告诉NSInvocation要做   所以,它有责任   释放它们。

(我还建议您阅读Memory Management Programming Guide了解其他一些此类模式和见解。)