/// <summary> /// 使用受保护的虚方法,考虑类继承调用 /// 非密封类修饰用protected virtual /// 密封类修饰用private /// </summary> /// <param name="isDispose"></param> protected virtual void Dispose(bool isDispose) { if (m_isDisposed) { return; } //显式的调用Dispose需要手工的清理对象本身托管资源,而终结器调用不需要手工清理 if (isDispose) { //清理托管资源 if (m_manageResource != null) { //对象被dispose不表示该对象为null m_manageResource.Dispose(); m_manageResource = null; } } //清理非托管资源 if (m_nativeResource != IntPtr.Zero) { Marshal.FreeHGlobal(m_nativeResource); m_nativeResource = IntPtr.Zero; } //指示资源已释放 m_isDisposed = true; }
/// <summary> /// 非密封类修饰用protected virtual /// 密封类修饰用private /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (derivedDisposed) { return; } if (disposing) { // 清理托管资源 if (derivedManagedResource != null) { derivedManagedResource.Dispose(); derivedManagedResource = null; } } // 清理非托管资源 if (derivedNativeResource != IntPtr.Zero) { Marshal.FreeHGlobal(derivedNativeResource); derivedNativeResource = IntPtr.Zero; } //调用父类的清理代码 base.Dispose(disposing); //让类型知道自己已经被释放 derivedDisposed = true; }