Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 568889d

Browse files
committed
pin the SubclassProc delegate
fix crash after reading email item
1 parent 9e1448f commit 568889d

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("1.1.0.0")]
37-
[assembly: AssemblyFileVersion("1.1.0.0")]
36+
[assembly: AssemblyVersion("1.2.0.0")]
37+
[assembly: AssemblyFileVersion("1.2.0.0")]
3838

ThisAddIn.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public partial class ThisAddIn {
3131
const int SW_MINIMIZE = 6;
3232

3333
IntPtr _hWnd = IntPtr.Zero, _scProc = IntPtr.Zero;
34+
SubclassProc _scDelegate;
3435
GCHandle _gch;
3536

3637
IntPtr MySubclassProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam, UIntPtr uIdSubclass, UIntPtr dwRefData) {
@@ -47,21 +48,26 @@ private void ThisAddIn_Startup(object sender, System.EventArgs e) {
4748
// before our module is unloaded to avoid crashes
4849
((Outlook.ApplicationEvents_11_Event)Application).Quit += ThisAddIn_Quit;
4950
if (((IOleWindow)Application.ActiveExplorer()).GetWindow(out _hWnd) == S_OK && _hWnd != IntPtr.Zero) {
50-
_gch = GCHandle.Alloc(this);
51-
_scProc = Marshal.GetFunctionPointerForDelegate((SubclassProc)MySubclassProc);
51+
_scDelegate = new SubclassProc(MySubclassProc);
52+
_gch = GCHandle.Alloc(_scProc);
53+
_scProc = Marshal.GetFunctionPointerForDelegate(_scDelegate);
5254
if (SetWindowSubclass(_hWnd, _scProc, UIntPtr.Zero, UIntPtr.Zero)) {
5355
return;
5456
}
57+
_scProc = IntPtr.Zero;
5558
_gch.Free();
59+
_scDelegate = null;
5660
}
5761
_hWnd = IntPtr.Zero;
5862
}
5963

6064
private void ThisAddIn_Quit() {
6165
if (_hWnd != IntPtr.Zero) {
6266
RemoveWindowSubclass(_hWnd, _scProc, UIntPtr.Zero);
63-
_hWnd = IntPtr.Zero;
67+
_scProc = IntPtr.Zero;
6468
_gch.Free();
69+
_scDelegate = null;
70+
_hWnd = IntPtr.Zero;
6571
}
6672
}
6773

0 commit comments

Comments
 (0)