dotnet / dotnet/dotnet-api-docs

IDisposable.Dispose Method – SafeHandle is used to release unmanged resource not otherwise

Open
#7,150 1 comment 0 reactions 0 assignees View on GitHub
area-System.Runtime Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

From [Remarks](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable.dispose?view=net-5.0#remarks)
> Because the Dispose method must be called explicitly, there is always a danger that the unmanaged resources will not be released, because the consumer of an object fails to call its Dispose method. There are two ways to avoid this:
• Wrap the managed resource in an object derived from System.Runtime.InteropServices.SafeHandle…

From [The SafeHandle alternative](https://docs.microsoft.com/en-us/dotnet/api/system.object.finalize?view=net-5.0#the-safehandle-alternative)
> Instead of implementing a finalizer for your class to release unmanaged resources, you can use an object that is derived from the System.Runtime.InteropServices.SafeHandle class to wrap your unmanaged resources, and then implement the dispose pattern without a finalizer.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.