Try to get NSDate through forwardInvocation

I have a problem.

I have a 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 an NSDate or NSData object class, it will not work on a 64-bit device. I get EXC_BAD_ACCESS code=1

And a message from NSZombie

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

But for another type object, it works. How can I solve this problem? Thanks you

0
ios exc-bad-access
source share
1 answer

obj can be freed immediately after the assignment, because it refers to unsafe_unretained , so -setReturnValue: deceives the pointer as an argument.

If unsafe_unretained is unavoidable for the installation path, (since -[NSInvocation getArgument:atIndex:] may not work correctly with a strong link, thanks @Tommy for this comment), you can handle the getter path differently with a strong link.

+4
source share

All Articles