Skip to content

Commit 4119ea6

Browse files
committed
Changed a implementation of the Dispose method.
1 parent e05719e commit 4119ea6

13 files changed

+249
-240
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.1.200"
3+
"version": "2.1.300"
44
}
55
}

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
4242
/// <summary>
4343
/// List of host items
4444
/// </summary>
45-
protected readonly Dictionary<string, object> _hostItems = new Dictionary<string, object>();
45+
protected Dictionary<string, object> _hostItems = new Dictionary<string, object>();
4646

4747
/// <summary>
4848
/// Last Active Script exception
@@ -77,14 +77,12 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
7777
/// <summary>
7878
/// List of document names
7979
/// </summary>
80-
private readonly Dictionary<UIntPtr, string> _documentNames =
81-
new Dictionary<UIntPtr, string>();
80+
private Dictionary<UIntPtr, string> _documentNames = new Dictionary<UIntPtr, string>();
8281

8382
/// <summary>
8483
/// List of debug documents
8584
/// </summary>
86-
private readonly Dictionary<UIntPtr, DebugDocument> _debugDocuments =
87-
new Dictionary<UIntPtr, DebugDocument>();
85+
private Dictionary<UIntPtr, DebugDocument> _debugDocuments = new Dictionary<UIntPtr, DebugDocument>();
8886

8987
/// <summary>
9088
/// Next source context
@@ -174,14 +172,6 @@ protected ActiveScriptJsEngineBase(JsEngineSettings settings, string clsid,
174172
}
175173
}
176174

177-
/// <summary>
178-
/// Destructs an instance of the Active Script engine
179-
/// </summary>
180-
~ActiveScriptJsEngineBase()
181-
{
182-
Dispose(false);
183-
}
184-
185175

186176
/// <summary>
187177
/// Checks a support of the JS engine on the machine
@@ -910,25 +900,45 @@ public override void CollectGarbage()
910900
/// Destroys object
911901
/// </summary>
912902
public override void Dispose()
913-
{
914-
Dispose(true /* disposing */);
915-
GC.SuppressFinalize(this);
916-
}
917-
918-
/// <summary>
919-
/// Destroys object
920-
/// </summary>
921-
/// <param name="disposing">Flag, allowing destruction of
922-
/// managed objects contained in fields of class</param>
923-
private void Dispose(bool disposing)
924903
{
925904
if (_disposedFlag.Set())
926905
{
906+
if (_debuggingStarted && _debugDocuments != null)
907+
{
908+
foreach (UIntPtr debugDocumentKey in _debugDocuments.Keys)
909+
{
910+
var debugDocumentValue = _debugDocuments[debugDocumentKey];
911+
debugDocumentValue.Close();
912+
}
913+
914+
_debugDocuments.Clear();
915+
_debugDocuments = null;
916+
}
917+
918+
if (_processDebugManagerWrapper != null)
919+
{
920+
_processDebugManagerWrapper.RemoveApplication(_debugApplicationCookie);
921+
922+
if (_debugApplicationWrapper != null)
923+
{
924+
_debugApplicationWrapper.Close();
925+
_debugApplicationWrapper = null;
926+
}
927+
928+
_processDebugManagerWrapper = null;
929+
}
930+
931+
if (_documentNames != null)
932+
{
933+
_documentNames.Clear();
934+
_documentNames = null;
935+
}
936+
927937
_dispatcher.Invoke(() =>
928938
{
929939
if (_dispatch != null)
930940
{
931-
ComHelpers.ReleaseComObject(ref _dispatch, !disposing);
941+
Marshal.ReleaseComObject(_dispatch);
932942
_dispatch = null;
933943
}
934944

@@ -939,30 +949,13 @@ private void Dispose(bool disposing)
939949
}
940950
});
941951

942-
if (disposing)
952+
if (_hostItems != null)
943953
{
944-
if (_debuggingStarted && _debugDocuments != null)
945-
{
946-
foreach (UIntPtr debugDocumentKey in _debugDocuments.Keys)
947-
{
948-
var debugDocumentValue = _debugDocuments[debugDocumentKey];
949-
debugDocumentValue.Close();
950-
}
951-
952-
_debugDocuments.Clear();
953-
}
954-
955-
if (_processDebugManagerWrapper != null)
956-
{
957-
_processDebugManagerWrapper.RemoveApplication(_debugApplicationCookie);
958-
_debugApplicationWrapper.Close();
959-
}
960-
961-
_documentNames?.Clear();
962-
_hostItems?.Clear();
963-
964-
_lastException = null;
954+
_hostItems.Clear();
955+
_hostItems = null;
965956
}
957+
958+
_lastException = null;
966959
}
967960
}
968961

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptWrapper32.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
55

66
using MsieJavaScriptEngine.ActiveScript.Debugging;
7+
using MsieJavaScriptEngine.Helpers;
78

89
namespace MsieJavaScriptEngine.ActiveScript
910
{
@@ -12,6 +13,21 @@ namespace MsieJavaScriptEngine.ActiveScript
1213
/// </summary>
1314
internal sealed class ActiveScriptWrapper32 : ActiveScriptWrapperBase
1415
{
16+
/// <summary>
17+
/// Pointer to an instance of 32-bit Active Script parser
18+
/// </summary>
19+
private IntPtr _pActiveScriptParse32;
20+
21+
/// <summary>
22+
/// Pointer to an instance of 32-bit Active Script debugger
23+
/// </summary>
24+
private IntPtr _pActiveScriptDebug32;
25+
26+
/// <summary>
27+
/// Pointer to an instance of 32-bit debug stack frame sniffer
28+
/// </summary>
29+
private IntPtr _pDebugStackFrameSniffer32;
30+
1531
/// <summary>
1632
/// Instance of 32-bit Active Script parser
1733
/// </summary>
@@ -37,14 +53,28 @@ internal sealed class ActiveScriptWrapper32 : ActiveScriptWrapperBase
3753
public ActiveScriptWrapper32(string clsid, ScriptLanguageVersion languageVersion, bool enableDebugging)
3854
: base(clsid, languageVersion, enableDebugging)
3955
{
56+
_pActiveScriptParse32 = ComHelpers.QueryInterface<IActiveScriptParse32>(_pActiveScript);
4057
_activeScriptParse32 = (IActiveScriptParse32)_activeScript;
58+
4159
if (_enableDebugging)
4260
{
61+
_pActiveScriptDebug32 = ComHelpers.QueryInterface<IActiveScriptDebug32>(_pActiveScript);
4362
_activeScriptDebug32 = (IActiveScriptDebug32)_activeScript;
63+
64+
_pDebugStackFrameSniffer32 = ComHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx32>(
65+
_pActiveScript);
4466
_debugStackFrameSniffer32 = _activeScript as IDebugStackFrameSnifferEx32;
4567
}
4668
}
4769

70+
/// <summary>
71+
/// Destructs an instance of the 32-bit Active Script wrapper
72+
/// </summary>
73+
~ActiveScriptWrapper32()
74+
{
75+
Dispose(false);
76+
}
77+
4878

4979
#region ActiveScriptWrapperBase overrides
5080

@@ -100,15 +130,36 @@ public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
100130

101131
#region IDisposable implementation
102132

133+
/// <summary>
134+
/// Destroys object
135+
/// </summary>
103136
public override void Dispose()
137+
{
138+
Dispose(true /* disposing */);
139+
GC.SuppressFinalize(this);
140+
}
141+
142+
/// <summary>
143+
/// Destroys object
144+
/// </summary>
145+
/// <param name="disposing">Flag, allowing destruction of
146+
/// managed objects contained in fields of class</param>
147+
protected override void Dispose(bool disposing)
104148
{
105149
if (_disposedFlag.Set())
106150
{
107-
_debugStackFrameSniffer32 = null;
108-
_activeScriptDebug32 = null;
109-
_activeScriptParse32 = null;
110-
111-
base.Dispose();
151+
if (disposing)
152+
{
153+
_debugStackFrameSniffer32 = null;
154+
_activeScriptDebug32 = null;
155+
_activeScriptParse32 = null;
156+
}
157+
158+
ComHelpers.ReleaseAndEmpty(ref _pDebugStackFrameSniffer32);
159+
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptDebug32);
160+
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptParse32);
161+
162+
base.Dispose(disposing);
112163
}
113164
}
114165

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptWrapper64.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
55

66
using MsieJavaScriptEngine.ActiveScript.Debugging;
7+
using MsieJavaScriptEngine.Helpers;
78

89
namespace MsieJavaScriptEngine.ActiveScript
910
{
@@ -12,6 +13,21 @@ namespace MsieJavaScriptEngine.ActiveScript
1213
/// </summary>
1314
internal sealed class ActiveScriptWrapper64 : ActiveScriptWrapperBase
1415
{
16+
/// <summary>
17+
/// Pointer to an instance of 64-bit Active Script parser
18+
/// </summary>
19+
private IntPtr _pActiveScriptParse64;
20+
21+
/// <summary>
22+
/// Pointer to an instance of 64-bit Active Script debugger
23+
/// </summary>
24+
private IntPtr _pActiveScriptDebug64;
25+
26+
/// <summary>
27+
/// Pointer to an instance of 64-bit debug stack frame sniffer
28+
/// </summary>
29+
private IntPtr _pDebugStackFrameSniffer64;
30+
1531
/// <summary>
1632
/// Instance of 64-bit Active Script parser
1733
/// </summary>
@@ -37,14 +53,28 @@ internal sealed class ActiveScriptWrapper64 : ActiveScriptWrapperBase
3753
public ActiveScriptWrapper64(string clsid, ScriptLanguageVersion languageVersion, bool enableDebugging)
3854
: base(clsid, languageVersion, enableDebugging)
3955
{
56+
_pActiveScriptParse64 = ComHelpers.QueryInterface<IActiveScriptParse64>(_pActiveScript);
4057
_activeScriptParse64 = (IActiveScriptParse64)_activeScript;
58+
4159
if (_enableDebugging)
4260
{
61+
_pActiveScriptDebug64 = ComHelpers.QueryInterface<IActiveScriptDebug64>(_pActiveScript);
4362
_activeScriptDebug64 = (IActiveScriptDebug64)_activeScript;
63+
64+
_pDebugStackFrameSniffer64 = ComHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx64>(
65+
_pActiveScript);
4466
_debugStackFrameSniffer64 = _activeScript as IDebugStackFrameSnifferEx64;
4567
}
4668
}
4769

70+
/// <summary>
71+
/// Destructs an instance of the 64-bit Active Script wrapper
72+
/// </summary>
73+
~ActiveScriptWrapper64()
74+
{
75+
Dispose(false);
76+
}
77+
4878

4979
#region ActiveScriptWrapperBase overrides
5080

@@ -100,15 +130,36 @@ public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
100130

101131
#region IDisposable implementation
102132

133+
/// <summary>
134+
/// Destroys object
135+
/// </summary>
103136
public override void Dispose()
137+
{
138+
Dispose(true /* disposing */);
139+
GC.SuppressFinalize(this);
140+
}
141+
142+
/// <summary>
143+
/// Destroys object
144+
/// </summary>
145+
/// <param name="disposing">Flag, allowing destruction of
146+
/// managed objects contained in fields of class</param>
147+
protected override void Dispose(bool disposing)
104148
{
105149
if (_disposedFlag.Set())
106150
{
107-
_debugStackFrameSniffer64 = null;
108-
_activeScriptDebug64 = null;
109-
_activeScriptParse64 = null;
110-
111-
base.Dispose();
151+
if (disposing)
152+
{
153+
_debugStackFrameSniffer64 = null;
154+
_activeScriptDebug64 = null;
155+
_activeScriptParse64 = null;
156+
}
157+
158+
ComHelpers.ReleaseAndEmpty(ref _pDebugStackFrameSniffer64);
159+
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptDebug64);
160+
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptParse64);
161+
162+
base.Dispose(disposing);
112163
}
113164
}
114165

0 commit comments

Comments
 (0)