We’ve been looking at why it seems like the CoreApplication. frees objects when their source apartment shuts down, and why that is just a mirage.
But this was all in the context of figuring out how to create a fake agile reference to an object that refuses to be marshaled, so let’s see if any of this helps us.
If you have an object that refuses to be marshaled (by implementing INoMarshal), then we learned that the RoGetAgileReference function cannot create an agile reference to it. And since the CoreApplication. uses RoGetAgileReference to store non-agile objects, trying to store such an object in the CoreApplication. fails with CO_.
So you’re back to square one: You have a non-marshalable object, so how do you create an agile reference to it, while still honoring the promise not to use the object from another apartment?
We can proceed with the same solution: Put the non-marshable object inside a marshalable object, and then use the marshalable wrapper.
The original solution merely carried the marshalable object around and used it to access the non-marshalable object. I guess we could instead store it in the CoreApplication. under a newly-generated key, and then remember the key. When it’s time to invoke the delegate, we go back to CoreApplication., look up the key, get the wrapper, and then use it to access the original non-marshalable object. And at destruction, we delete the entry from the CoreApplication..
I mean, I guess you could do that, but it seems like an awful lot of unnecessary work. You have to remember something, either the marshalable wrapper or the key to the marshalable wrapper. May as well just remember the marshalable wrapper itself to avoid unnecessary complexity. The CoreApplication. is not adding any value.
Thank you! I didn’t expect IAgileReference to auto-release the object on apartment shutdown *. I thought it was responsibility of the user
to release IAgileReference when the originating apartment was still alive. This should be included in the MSDN documentation!
So IAgileReference is a “reference that is strong, but which releases when the originating apartment tears down”. Yet the GIT wins in terms of simplicity.
* IAgileReference created via RoGetAgileReference on non-agile objects
* The “source” apartment shutdown; that is, the apartment from which RoGetAgileReference is called.
Best,
Luca