dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.79k stars 193 forks source link

ReleaseComObject on ComReleaser.Dispose() #412

Closed cuft closed 1 year ago

cuft commented 1 year ago

It seems it was just missing here...

dahall commented 1 year ago

This was actually the code I expressly removed from the original implementation. After a lot of testing, I found that ComReleaser is really quite unnecessary. Very regularly, .NET released the COM object before this Dispose method was even called which resulted in a memory exception.

cuft commented 1 year ago

Interesting... However, i.e. IPropSetStorageTest fails without ReleaseComObject after writing. It returns STG_E_ACCESSDENIED on the reading trial.

Couldn't the memory issues be caused by multiple Dispose calls? An already disposed check would help then.

dahall commented 1 year ago

For me, the problem was caused when the interface was dereferenced/released external to ComReleaser. And, unfortunately, determining when that happened was not consistent. In my code, I go old-school and manually control references rather than betting on a disposal pattern. If I could find a way to prevent crashes, my preference would be to call FinalReleaseComObject in a dispose pattern.