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);