Skip to content

IDISP007 is triggering for an out parameter that is NOT injected and indeed MUST be disposed #580

@smaillet

Description

@smaillet

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions