-1

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?

1 Answers1

0

The __bridge_retained cast looks wrong. __bridge_retained gives 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 with alloc, new, retain, copy, or mutableCopy, and so should not return a retained reference.

Changing it to a simple __bridge cast should make it balanced again.

newacct
  • 119,665
  • 29
  • 163
  • 224