public void _01_DisposeMechanismTest() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) Assert.Inconclusive("Test cannot be executed on this platform"); byte[] parameter = new byte[8]; System.Random rng = new Random(); rng.NextBytes(parameter); // Unmanaged memory for mechanism parameter stored in low level CK_MECHANISM struct // is allocated by constructor of Mechanism class. Mechanism mechanism1 = new Mechanism(CKM.CKM_DES_CBC, parameter); // Do something interesting with mechanism // This unmanaged memory is freed by Dispose() method. mechanism1.Dispose(); // Mechanism class can be used in using statement which defines a scope // at the end of which an object will be disposed (and unmanaged memory freed). using (Mechanism mechanism2 = new Mechanism(CKM.CKM_DES_CBC, parameter)) { // Do something interesting with mechanism } #pragma warning disable 0219 // Explicit calling of Dispose() method can also be ommitted // and this is the prefered way how to use Mechanism class. Mechanism mechanism3 = new Mechanism(CKM.CKM_DES_CBC, parameter); // Do something interesting with mechanism // Dispose() method will be called (and unmanaged memory freed) by GC eventually // but we cannot be sure when will this occur. #pragma warning restore 0219 }