3

释放 NSInvocation 时是否释放了保留的参数,或者我是否需要手动释放 NSInvocation 参数列表中的对象?

4

2 回答 2

5

“保留的论点”?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

于 2011-06-23T05:59:58.513 回答
2

通过谷歌,我找到了这个对话,它解释了你不需要发布论点的关键原因:

所以我决定 -retainArguments,并假设这意味着我应该在完成目标和参数后释放它们。

正如你所发现的,不,你不应该那样做。您没有保留目标和参数。您已经告诉 NSInvocation 这样做,然后它有责任释放它们。

(我还建议您阅读内存管理编程指南以了解其他一些此类模式和见解。)

于 2011-06-23T05:59:12.387 回答