public static IDisposable ToAutoClear <T>(this T[] buffer) where T : struct { IDisposable ret = null; try { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { var handle = buffer.Pinned(); ret = new FinalizerAction(() => { try { MemoryClearArray(buffer); } finally { handle.Dispose(); } }); } return(ret); } catch (ThreadAbortException) { ret?.Dispose(); throw; } catch (Exception) { ret?.Dispose(); throw; } }
public static IDisposable ToAutoClear(this string str) { IDisposable ret = null; try { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { var handle = str.Pinned(); ret = new FinalizerAction(() => { try { MemoryClearString(str); } finally { handle.Dispose(); } }); } return(ret); } catch (ThreadAbortException) { ret?.Dispose(); throw; } catch (Exception) { ret?.Dispose(); throw; } }
public static IDisposable Pinned <T>(this T obj, out IntPtr ptr) where T : class { IDisposableAction ret = null; RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { var handle = GCHandle.Alloc(obj, GCHandleType.Pinned); ret = new FinalizerAction(() => handle.Free()); ptr = handle.AddrOfPinnedObject(); } return(ret); }