use*_*876 8 cocoa-touch memory-management objective-c nsinvocation ios
所以我来自Java世界,我们对内存管理问题一无所知.在大多数情况下,ARC已经救了我的屁股,但这里有些让我难过的东西.基本上我使用NSInvocations来做一些事情,在进行下面的代码修改之前,我遇到了一些讨厌的内存问题.由于我做了这些修改,内存崩溃已经消失,但我通常非常害怕我不理解的代码.我这样做了吗?
之前:各种内存问题:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[target class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:target];
[invocation setArgument:&data atIndex:2];
[invocation setArgument:&arg atIndex:3];
[invocation invoke];
NSString *returnValue;
[invocation getReturnValue:&returnValue];
Run Code Online (Sandbox Code Playgroud)
之后:没有内存问题,但我不确定我是否正确:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[target class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:target];
[invocation setArgument:&data atIndex:2];
[invocation setArgument:&arg atIndex:3];
[invocation invoke];
CFTypeRef result;
[invocation getReturnValue:&result];
if (result)
CFRetain(result);
NSString *returnValue = (__bridge_transfer NSString *)result;
Run Code Online (Sandbox Code Playgroud)
编辑:
我只想根据下面的答案添加,我使用了objc_msgSend,因此:
NSString * returnValue = objc_msgSend(target, selector, data, arg);
Run Code Online (Sandbox Code Playgroud)
它解决了所有内存问题,而且看起来更简单.如果您发现此问题,请发表评论.
我会这样回答你的问题:不要使用NSInvocation.如果可能的话,这是一个友好的建议,以避免这种情况.
在Objective-C中有很多很好的方法可以做回调,这里有两个可能对你有用的方法:
performSelector:最多2个对象参数,使用以下方法调用:
[target performSelector:selector withObject:data withObject:args];
Run Code Online (Sandbox Code Playgroud)另外,当我需要调用带有4个参数的选择器时,我仍然不使用NSIvocation,而是objc_msgSend直接调用:
id returnValue = objc_msgSend(target, selector, data, /* argument1, argument2, ... */);
Run Code Online (Sandbox Code Playgroud)
简单.
编辑:随着objc_msgSend你必须小心的返回值.如果您的方法返回一个对象,请使用上面的方法.如果它返回一个基本类型,则需要转换该objc_msgSend方法,以便编译器知道发生了什么(请参阅此链接).这是一个带有一个参数并返回BOOL的方法的示例:
// Cast the objc_msgSend function to a function named BOOLMsgSend which takes one argument and has a return type of BOOL.
BOOL (*BOOLMsgSend)(id, SEL, id) = (typeof(BOOLMsgSend)) objc_msgSend;
BOOL ret = BOOLMsgSend(target, selector, arg1);
Run Code Online (Sandbox Code Playgroud)
如果你的方法返回一个结构,事情会有点复杂.您可能(但并非总是)需要使用objc_msgSend_stret- 请参阅此处了解更多信息.
编辑: - 这行必须添加到代码中,否则Xcode会抱怨:
#import <objc/message.h>
Run Code Online (Sandbox Code Playgroud)
要么
@import ObjectiveC.message;
Run Code Online (Sandbox Code Playgroud)