From 51985ef88e1ede47291168330731d18a3f8085c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Nils=20Kuklau?= Date: Thu, 19 Apr 2018 13:19:10 +0200 Subject: [PATCH] Update TaskDialog.cs Possible fix for #4 --- src/TaskDialogLib/TaskDialog.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/TaskDialogLib/TaskDialog.cs b/src/TaskDialogLib/TaskDialog.cs index b6d66ca..7c0b910 100644 --- a/src/TaskDialogLib/TaskDialog.cs +++ b/src/TaskDialogLib/TaskDialog.cs @@ -76,6 +76,9 @@ public class TaskDialog : DependencyObject Boolean sizeToContent; Boolean useDefaultIcon; Boolean verificationChecked; + + // This needs to be a class member so that GC doesn't free it prematurely. + TaskDialogCallbackProc callback; IconHandle iconHandle; IconHandle footerIconHandle; @@ -165,7 +168,7 @@ public class TaskDialog : DependencyObject #endregion - #region Constructors + #region Constructors and Destructors /// /// Initializes a new instance of the TaskDialog class. @@ -178,6 +181,14 @@ public TaskDialog() useDefaultIcon = true; } + /// + /// Cleans up the TaskDialog class. + /// + ~TaskDialog() + { + callback = null; + } + #endregion #region Events @@ -762,7 +773,7 @@ public TaskDialogResult Show() NativeMethods.TASKDIALOGCONFIG taskDialogConfig = new NativeMethods.TASKDIALOGCONFIG(); taskDialogConfig.cbSize = (UInt32)Marshal.SizeOf(typeof(NativeMethods.TASKDIALOGCONFIG)); taskDialogConfig.hInstance = NativeMethods.GetModuleHandle(); - taskDialogConfig.pfCallback = TaskDialogProc; + taskDialogConfig.pfCallback = callback = TaskDialogProc; Debug.Assert(taskDialogConfig.hInstance != IntPtr.Zero);