﻿<?xml version="1.0" encoding="utf-8"?><Type Name="PreferSafeHandleRule" FullName="Gendarme.Rules.BadPractice.PreferSafeHandleRule"><TypeSignature Language="C#" Value="public sealed class PreferSafeHandleRule : Gendarme.Framework.Rule, Gendarme.Framework.ITypeRule" /><TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit PreferSafeHandleRule extends Gendarme.Framework.Rule implements class Gendarme.Framework.IRule, class Gendarme.Framework.ITypeRule" /><AssemblyInfo><AssemblyName>Gendarme.Rules.BadPractice</AssemblyName><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><Base><BaseTypeName>Gendarme.Framework.Rule</BaseTypeName></Base><Interfaces><Interface><InterfaceName>Gendarme.Framework.ITypeRule</InterfaceName></Interface></Interfaces><Attributes><Attribute><AttributeName>Gendarme.Framework.FxCopCompatibility("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")</AttributeName></Attribute><Attribute><AttributeName>Gendarme.Framework.Problem("The type uses System.IntPtr or System.UIntPtr instead of System.Runtime.InteropServices.SafeHandle.")</AttributeName></Attribute><Attribute><AttributeName>Gendarme.Framework.Solution("Consider changing the code to use SafeHandle.")</AttributeName></Attribute></Attributes><Docs><summary>
             In general it is best to interop with native code using 
             <c>System.Runtime.InteropServices.SafeHandle</c> instead of 
             <c>System.IntPtr</c> or <c>System.UIntPtr</c> because:
             <list type="bullet"><item><description>SafeHandles are type safe.</description></item><item><description>SafeHandles are guaranteed to be disposed of during 
             exceptional conditions like a thread aborting unexpectedly or a stack 
             overflow.</description></item><item><description>SafeHandles are not vulnerable to reycle attacks.</description></item><item><description>You don't need to write a finalizer which can be tricky 
             to do because they execute within their own thread, may execute on 
             partially constructed objects, and normally tear down the application
             if you allow an exception to escape from them.</description></item></list></summary><remarks>This rule is available since Gendarme 2.6</remarks><example>
             Bad example:
             <code>
             using System.Runtime.InteropServices;
             using System.Security;
             using System.Security.Permissions;
             
             // If cleaning up the native resource in a timely manner is important you can
             // implement IDisposable.
             public sealed class Database {
             	~Database ()
             	{
             		// This will execute even if the ctor throws so it is important to check
             		// to see if the fields are initialized.
             		if (m_database != IntPtr.Zero) {
             			NativeMethods.sqlite3_close (m_database);
             		}
             	}
             
             	public Database (string path)
             	{		
             		NativeMethods.OpenFlags flags = NativeMethods.OpenFlags.READWRITE | NativeMethods.OpenFlags.CREATE;
             		int err = NativeMethods.sqlite3_open_v2 (path, out m_database, flags, IntPtr.Zero);
             		// handle errors
             	}
             	
             	// exec and query methods would go here
             							
             	[SuppressUnmanagedCodeSecurity]
             	private static class NativeMethods {
             		[Flags]
             		public enum OpenFlags : int {
             			READONLY = 0x00000001,
             			READWRITE = 0x00000002,
             			CREATE = 0x00000004,
             			// ...
             		}
             		
             		[DllImport ("sqlite3")]
             		public static extern int sqlite3_close (IntPtr db);
             					
             	   	[DllImport ("sqlite3")]
             		public static extern int sqlite3_open_v2 (string fileName, out IntPtr db, OpenFlags flags, IntPtr module);
             	}
             	
                 private IntPtr m_database;
             }
             </code></example><example>
             Good example:
             <code>
             using System.Runtime.ConstrainedExecution;
             using System.Runtime.InteropServices;
             using System.Security;
             using System.Security.Permissions;
             
             // If cleaning up the native resource in a timely manner is important you can
             // implement IDisposable, but you do not need to implement a finalizer because
             // SafeHandle will take care of the cleanup.
             internal sealed class Database {
             	public Database (string path)
             	{
             		NativeMethods.OpenFlags flags = NativeMethods.OpenFlags.READWRITE | NativeMethods.OpenFlags.CREATE;
             		m_database = new SqlitePtr (path, flags);
             	}
             	
             	// exec and query methods would go here
             	
             	// This corresponds to a native sqlite3*.
             	[SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
             	[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
             	private sealed class SqlitePtr : SafeHandle {
             		public SqlitePtr (string path, NativeMethods.OpenFlags flags) : base (IntPtr.Zero, true)
             		{		
             			int err = NativeMethods.sqlite3_open_v2 (path, out handle, flags, IntPtr.Zero);
             			// handle errors
             		}
             		
             		public override bool IsInvalid { 
             			get {
             				return (handle == IntPtr.Zero);
             			}
             		}
             		
             		// This will not be called if the handle is invalid. Note that this method should not throw.
             		[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
             		protected override bool ReleaseHandle ()
             		{
             			NativeMethods.sqlite3_close (this);
             			return true;
             		}
             	}
             
             	[SuppressUnmanagedCodeSecurity]
             	private static class NativeMethods {
             		[Flags]
             		public enum OpenFlags : int {
             			READONLY = 0x00000001,
             			READWRITE = 0x00000002,
             			CREATE = 0x00000004,
             			// ...
             		}
             		
             		[DllImport ("sqlite3")]
             		public static extern int sqlite3_close (SqlitePtr db);
             				
             		// Open must take an IntPtr but all other methods take a type safe SqlitePtr.
             		[DllImport ("sqlite3")]
             		public static extern int sqlite3_open_v2 (string fileName, out IntPtr db, OpenFlags flags, IntPtr module);
             	}
             
             	private SqlitePtr m_database;
             }
             </code></example></Docs><Members><Member MemberName=".ctor"><MemberSignature Language="C#" Value="public PreferSafeHandleRule ();" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><Parameters /><Docs><summary>To be added.</summary><remarks>To be added.</remarks></Docs></Member><Member MemberName="CheckType"><MemberSignature Language="C#" Value="public Gendarme.Framework.RuleResult CheckType (Mono.Cecil.TypeDefinition type);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance valuetype Gendarme.Framework.RuleResult CheckType(class Mono.Cecil.TypeDefinition type) cil managed" /><MemberType>Method</MemberType><AssemblyInfo><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><ReturnValue><ReturnType>Gendarme.Framework.RuleResult</ReturnType></ReturnValue><Parameters><Parameter Name="type" Type="Mono.Cecil.TypeDefinition" /></Parameters><Docs><param name="type">To be added.</param><summary>To be added.</summary><returns>To be added.</returns><remarks>To be added.</remarks></Docs></Member><Member MemberName="Initialize"><MemberSignature Language="C#" Value="public override void Initialize (Gendarme.Framework.IRunner runner);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance void Initialize(class Gendarme.Framework.IRunner runner) cil managed" /><MemberType>Method</MemberType><AssemblyInfo><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><ReturnValue><ReturnType>System.Void</ReturnType></ReturnValue><Parameters><Parameter Name="runner" Type="Gendarme.Framework.IRunner" /></Parameters><Docs><param name="runner">To be added.</param><summary>To be added.</summary><remarks>To be added.</remarks></Docs></Member></Members></Type>