-
Couldn't load subscription status.
- Fork 31
Description
We have a static "factory" method that follows the general standard .NET "try" pattern but does not return a bool, instead it returns a custom status that may include an error message (In real world this is a SafeHandle for a native interop allocated value that needs a dispose) another SafeHandle derived type is an out parameter. Any use of this is triggering IDISP007.
Example:
[DllImport]
private static extern CustomSafeHandleDerivedType Something(out Microsoft.Win32.SafeHandles.SafeFileHandle h)Using that API will cause IDISP007
using var errorRef = Something(out Microsoft.Win32.SafeHandles.SafeFileHandle h);
using(h) // <--- IDISP007 is triggered here
{
}But since this is Created by the interop support (DllImport OR LibraryImport) it MUST be disposed of. IDISP007 is saying DON'T dispose it but that would result in leaking the handle until process exit. While we can use pragmas to ignore the warning, doing so in EVERY case of this is a tedious effort and defeats the point of having an analyzer to check for this sort of thing.