I try to use NSProxy to wrap object and make an proxy instance in (forwardInvocation:) as invocation' retValue, but all proxyon instance from can't be release in ARC. I've been troubled for a long time.
demo at github:https://github.com/JorrisRaghan/proxyon
you can see console log:
DEALLOC PPObject:...
DEALLOC Proxyon:Class< PPObject >
but without DEALLOC Proxyon:id< PPObject >".
you also can use Instruments to check memory leak.
so I need your help to solve it, thank you!
Update: I found that this leak is from forwardInvocation:
void *proxyon = (__bridge_retained void *)[Proxyon proxyonWithInstance:obj];
[anInvocation setReturnValue:&proxyon];
I set PPObject as retVal instead of Proxyon instance, and leak happened in PPObject, so I guess -[NSInvocation setReturnValue:] is the key.But how to solve it?
The
__bridge_retainedcast looks wrong.__bridge_retainedgives you a retained reference. You then set this as the return value of the invocation, without a balancing release, so the effect is that the call is returning a retained reference. But the name of the method that this is for (instanceWithIdentifier:) does not begin withalloc,new,retain,copy, ormutableCopy, and so should not return a retained reference.Changing it to a simple
__bridgecast should make it balanced again.