Closed smourier closed 5 months ago
The resolution will cause a breaking change, as we introduce the following changes:
PropVariant
is not sealed anymorePropVariant
by default still clears itself at Dispose
time but it's now overridablePROPVARIANT
type deriving from PropVariant
that doesn't clear itself at Dispose
time.PROPVARIANT
in lieu of PropVariant
.Relevant code:
class PropVariant
{
public virtual bool ClearOnDispose => true;
// Detached is just a raw structure copy (doing nothing special, no AddRef on sub objects, etc.)
public PROPVARIANT Detached => new PROPVARIANT { <copy internal data>};
....
public void Dispose()
{
if (ClearOnDispose)
{
Clear(false);
}
GC.SuppressFinalize(this);
}
}
public class PROPVARIANT : PropVariant
{
public override bool ClearOnDispose => false;
...
}
Now, to call a COM native method like this:
HRESULT DoSomething(PROPVARIANT pv);
You'd use this:
using (var pv = new PropVariant(<something>))
{
myObj.DoSomething(pv.Detached).ThrowOnError();
}
The
PropVariant
type (a class) maps the native PROPVARIANT type.However, the actual (in version 1.15.0.2 ) version clears its inner data when it's disposed (using native
PropVariantClear
function)The problem is when this type is declared as a parameter (in or out) of a COM method, since the way it will be freed cannot be controlled, it will just happend "at GC collection time" which can cause AV issues difficult to track.