mono/mono — pull request #537 by madewokherd (2013-01-23 20:41:07+00:00)
Title: cominterop: Free IMarshal interface with the CCW.

Currently, we create an IMarshal interface for COM wrappers, and free it when the wrapper's refcount becomes 0. However, the COM wrapper sticks around and keeps the IMarshal interface, reusing the freed object if IMarshal is ever requested again.

We could create a new IMarshal interface every time, but I think it's better to use the same lifetime for it as the COM wrapper. COM aggregation rules are set up so that the IMarshal implementation can cope just fine with the container object having a refcount of 0, and it will keep a reference to our object as needed when we query for IMarshal. The overhead of keeping that object around should be very small.

The addref/release around CoCreateFreeThreadedMarshaler isn't necessary, as COM aggregation rules handle this just fine. (And there's no chance that it fixes some threading issue. If it's possible for the COM wrapper to go away in that time, which I don't believe is the case, the addref is just as unsafe as the following call.)