Reputation: 1253
While testing my code, I came across a behavior that I find strange.
This:
if (_sampGrabber != null)
{
Marshal.ReleaseComObject(_sampGrabber);
_sampGrabber = null;
}
yields _sampGrabber==null, whereas after
ReleaseIfNotNull(_sampGrabber);
_sampGrabber still has its orignal value when using
public static int ReleaseIfNotNull(object comObject)
{
int hr = 0;
if (comObject != null)
{
hr = Marshal.ReleaseComObject(comObject);
comObject = null;
}
return hr;
}
_sampGrabber is a DirectShow SampleGrabber filter interface.
I am interested in an explanation for this behaviour. I came across it when writing tests using the Visual Studio Test Framework.
Upvotes: 2
Views: 95
Reputation: 107277
If you need to change the reference, you will need to change the signature like so:
public static int ReleaseIfNotNull(ref object comObject)
Changing the local reference to comObject
inside ReleaseIfNotNull
affects only its own (stack) copy, and won't affect any other references to it. MSDN on ref
and out
Upvotes: 4