Skip to content

Commit 4416643

Browse files
committed
Changed a implementation of the Dispose method
1 parent 3e0715b commit 4416643

File tree

10 files changed

+231
-120
lines changed

10 files changed

+231
-120
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript (http://github.com/paulcbetts/SassAndCoffee), Chakra Sample Hosts (http://github.com/panopticoncentral/chakra-host) and jsrt-dotnet (http://github.com/robpaveza/jsrt-dotnet).</description>
1414
<summary>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).</summary>
15-
<releaseNotes>Fixed a error #18 “Block finalizer solved?”.</releaseNotes>
15+
<releaseNotes>Changed a implementation of the `Dispose` method.</releaseNotes>
1616
<copyright>Copyright (c) 2012-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1717
<language>en-US</language>
1818
<tags>JavaScript ECMAScript MSIE IE Edge Chakra</tags>

NuGet/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
Fixed a error #18 “Block finalizer solved?”.
24+
Changed a implementation of the `Dispose` method.
2525

2626
============
2727
PROJECT SITE

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
3232
/// <summary>
3333
/// List of host items
3434
/// </summary>
35-
private readonly Dictionary<string, object> _hostItems = new Dictionary<string, object>();
35+
private Dictionary<string, object> _hostItems = new Dictionary<string, object>();
3636

3737
/// <summary>
3838
/// Last Active Script exception
@@ -67,8 +67,7 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
6767
/// <summary>
6868
/// List of debug documents
6969
/// </summary>
70-
private readonly Dictionary<UIntPtr, DebugDocument> _debugDocuments =
71-
new Dictionary<UIntPtr, DebugDocument>();
70+
private Dictionary<UIntPtr, DebugDocument> _debugDocuments = new Dictionary<UIntPtr, DebugDocument>();
7271

7372
/// <summary>
7473
/// Next source context
@@ -143,14 +142,6 @@ protected ActiveScriptJsEngineBase(JsEngineMode engineMode, bool enableDebugging
143142
}
144143
}
145144

146-
/// <summary>
147-
/// Destructs an instance of the Active Script engine
148-
/// </summary>
149-
~ActiveScriptJsEngineBase()
150-
{
151-
Dispose(false);
152-
}
153-
154145

155146
/// <summary>
156147
/// Checks a support of the JS engine on the machine
@@ -720,61 +711,56 @@ public override void CollectGarbage()
720711
/// Destroys object
721712
/// </summary>
722713
public override void Dispose()
723-
{
724-
Dispose(true /* disposing */);
725-
GC.SuppressFinalize(this);
726-
}
727-
728-
/// <summary>
729-
/// Destroys object
730-
/// </summary>
731-
/// <param name="disposing">Flag, allowing destruction of
732-
/// managed objects contained in fields of class</param>
733-
private void Dispose(bool disposing)
734714
{
735715
if (_disposedFlag.Set())
736716
{
737-
_dispatcher.Invoke(() =>
717+
if (_debuggingStarted && _debugDocuments != null)
738718
{
739-
if (_dispatch != null)
719+
foreach (UIntPtr debugDocumentKey in _debugDocuments.Keys)
740720
{
741-
ComHelpers.ReleaseComObject(ref _dispatch, !disposing);
742-
_dispatch = null;
721+
var debugDocumentValue = _debugDocuments[debugDocumentKey];
722+
debugDocumentValue.Close();
743723
}
744724

745-
if (_activeScriptWrapper != null)
746-
{
747-
_activeScriptWrapper.Dispose();
748-
_activeScriptWrapper = null;
749-
}
750-
});
725+
_debugDocuments.Clear();
726+
_debugDocuments = null;
727+
}
751728

752-
if (disposing)
729+
if (_processDebugManagerWrapper != null)
753730
{
754-
if (_debuggingStarted && _debugDocuments != null)
755-
{
756-
foreach (UIntPtr debugDocumentKey in _debugDocuments.Keys)
757-
{
758-
var debugDocumentValue = _debugDocuments[debugDocumentKey];
759-
debugDocumentValue.Close();
760-
}
731+
_processDebugManagerWrapper.RemoveApplication(_debugApplicationCookie);
761732

762-
_debugDocuments.Clear();
733+
if (_debugApplicationWrapper != null)
734+
{
735+
_debugApplicationWrapper.Close();
736+
_debugApplicationWrapper = null;
763737
}
764738

765-
if (_processDebugManagerWrapper != null)
739+
_processDebugManagerWrapper = null;
740+
}
741+
742+
_dispatcher.Invoke(() =>
743+
{
744+
if (_dispatch != null)
766745
{
767-
_processDebugManagerWrapper.RemoveApplication(_debugApplicationCookie);
768-
_debugApplicationWrapper.Close();
746+
Marshal.ReleaseComObject(_dispatch);
747+
_dispatch = null;
769748
}
770749

771-
if (_hostItems != null)
750+
if (_activeScriptWrapper != null)
772751
{
773-
_hostItems.Clear();
752+
_activeScriptWrapper.Dispose();
753+
_activeScriptWrapper = null;
774754
}
755+
});
775756

776-
_lastException = null;
757+
if (_hostItems != null)
758+
{
759+
_hostItems.Clear();
760+
_hostItems = null;
777761
}
762+
763+
_lastException = null;
778764
}
779765
}
780766

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptWrapper32.cs

Lines changed: 55 additions & 4 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>
@@ -36,17 +52,31 @@ internal sealed class ActiveScriptWrapper32 : ActiveScriptWrapperBase
3652
public ActiveScriptWrapper32(JsEngineMode engineMode, bool enableDebugging)
3753
: base(engineMode, enableDebugging)
3854
{
55+
_pActiveScriptParse32 = ComHelpers.QueryInterface<IActiveScriptParse32>(_pActiveScript);
3956
_activeScriptParse32 = (IActiveScriptParse32)_activeScript;
57+
4058
if (_enableDebugging)
4159
{
60+
_pActiveScriptDebug32 = ComHelpers.QueryInterface<IActiveScriptDebug32>(_pActiveScript);
4261
_activeScriptDebug32 = (IActiveScriptDebug32)_activeScript;
62+
4363
if (engineMode == JsEngineMode.Classic)
4464
{
65+
_pDebugStackFrameSniffer32 = ComHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx32>(
66+
_pActiveScript);
4567
_debugStackFrameSniffer32 = _activeScript as IDebugStackFrameSnifferEx32;
4668
}
4769
}
4870
}
4971

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

5181
#region ActiveScriptWrapperBase overrides
5282

@@ -102,15 +132,36 @@ public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
102132

103133
#region IDisposable implementation
104134

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

113-
base.Dispose();
164+
base.Dispose(disposing);
114165
}
115166
}
116167

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptWrapper64.cs

Lines changed: 55 additions & 4 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>
@@ -36,17 +52,31 @@ internal sealed class ActiveScriptWrapper64 : ActiveScriptWrapperBase
3652
public ActiveScriptWrapper64(JsEngineMode engineMode, bool enableDebugging)
3753
: base(engineMode, enableDebugging)
3854
{
55+
_pActiveScriptParse64 = ComHelpers.QueryInterface<IActiveScriptParse64>(_pActiveScript);
3956
_activeScriptParse64 = (IActiveScriptParse64)_activeScript;
57+
4058
if (_enableDebugging)
4159
{
60+
_pActiveScriptDebug64 = ComHelpers.QueryInterface<IActiveScriptDebug64>(_pActiveScript);
4261
_activeScriptDebug64 = (IActiveScriptDebug64)_activeScript;
62+
4363
if (engineMode == JsEngineMode.Classic)
4464
{
65+
_pDebugStackFrameSniffer64 = ComHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx64>(
66+
_pActiveScript);
4567
_debugStackFrameSniffer64 = _activeScript as IDebugStackFrameSnifferEx64;
4668
}
4769
}
4870
}
4971

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

5181
#region ActiveScriptWrapperBase overrides
5282

@@ -102,15 +132,36 @@ public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
102132

103133
#region IDisposable implementation
104134

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

113-
base.Dispose();
164+
base.Dispose(disposing);
114165
}
115166
}
116167

0 commit comments

Comments
 (0)