Try get NSDate through forwardInvocation

南楼画角 提交于 2019-12-11 18:50:18

问题


I have a problem.

I have method

- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL sel = anInvocation.selector;
NSString *prop = [self.class fieldNameForSelector:sel];
if (prop) {
    BOOL setter = [self isSetter:sel];
    __unsafe_unretained id obj;
    if (setter) {
        [anInvocation getArgument:&obj atIndex:2];
        [self setValue:obj forKey:prop];

    } else {
        obj = [self valueForKey:prop];
        [anInvocation setReturnValue:&obj];
    }
} else {
    [super forwardInvocation:anInvocation];
}

}

But if I try to get object class NSDate or NSData it's doesn't work in 64-bit device. I get EXC_BAD_ACCESS code=1

And message from NSZombie

[__NSDate retain]: message sent to deallocated instance 0x171e00d50

But for another type object it works. How I can resolved this problem? Thank you


回答1:


obj could deallocate right after assignment, because it's an unsafe_unretained reference, so -setReturnValue: gets dangling pointer as an argument.

If unsafe_unretained is unavoidable for setter path, (since -[NSInvocation getArgument:atIndex:] may not work properly with strong reference, thanks @Tommy for noting this), you could handle getter path differently, with strong reference.



来源:https://stackoverflow.com/questions/25009990/try-get-nsdate-through-forwardinvocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!