From 28c0d30bc7659efdd44ab786fb7c49c8d3ab93fe Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Sun, 14 Jul 2024 15:59:22 -0700 Subject: [PATCH] Add DO/AVOID for SafeHandles and Finalizers(#41746) Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/standard/native-interop/best-practices.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/standard/native-interop/best-practices.md b/docs/standard/native-interop/best-practices.md index 64ffed3feea46..ca439797deb37 100644 --- a/docs/standard/native-interop/best-practices.md +++ b/docs/standard/native-interop/best-practices.md @@ -23,6 +23,8 @@ The guidance in this section applies to all interop scenarios. - ✔️ CONSIDER using to pool your native array buffers. - ✔️ CONSIDER wrapping your P/Invoke declarations in a class with the same name and capitalization as your native library. - This allows your `[LibraryImport]` or `[DllImport]` attributes to use the C# `nameof` language feature to pass in the name of the native library and ensure that you didn't misspell the name of the native library. +- ✔️ DO use `SafeHandle` handles to manage lifetime of objects that encapsulate unmanaged resources. For more information, see [Cleaning up unmanaged resources](../garbage-collection/unmanaged.md). +- ❌ AVOID finalizers to manage lifetime of objects that encapsulate unmanaged resources. For more information, see [Implement a Dispose method](../garbage-collection/implementing-dispose.md). ## LibraryImport attribute settings