Skip to content

The dispose pattern docs should not use an IntPtr.Zero handle #44201

Closed
@antonfirsov

Description

@antonfirsov

Describe the issue or suggestion

The following doc was likely created in .NET Framework times, where the BaseClassWithSafeHandle code example creates and closes an IntPTr.Zero handle, which is NOP on Windows: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern

The problem is that on Unix FD 0 corresponds to stdnin which means that disposing BaseClassWithSafeHandle will close the standard input. What's even worse, that it will free up FD 0 for usage in other places, eg. Socket meaning that creating and disposing BaseClassWithSafeHandle several times may close the handle under an existing socket leading to unwanted side effects.

There were cases where users left the dummy code from the sample in their codebase, eventually leading to hard-to-diagnose production errors, see dotnet/runtime#56750, dotnet/runtime#64305, fabian-blum/AspNetCore.Identity.LiteDB#14.

We should change this example.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⌚ Not TriagedNot triagedin-prThis issue will be closed (fixed) by an active pull request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions