Skip to content

Commit e44ce80

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore changed a implementation of the Dispose method
1 parent 68763fc commit e44ce80

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>Fixed a error #34 “Finalazier thread is blocked because of JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine”.</releaseNotes>
23+
<releaseNotes>Changed a implementation of the `Dispose` method.</releaseNotes>
2424
<copyright>Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2525
<language>en-US</language>
2626
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
Fixed a error #34 “Finalazier thread is blocked because of
34-
JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine”.
33+
Changed a implementation of the `Dispose` method.
3534

3635
=============
3736
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
5050
/// <summary>
5151
/// Set of external objects
5252
/// </summary>
53-
private readonly HashSet<object> _externalObjects = new HashSet<object>();
53+
private HashSet<object> _externalObjects = new HashSet<object>();
5454

5555
/// <summary>
5656
/// Callback for finalization of external object
@@ -65,12 +65,12 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
6565
/// <summary>
6666
/// List of native function callbacks
6767
/// </summary>
68-
private readonly HashSet<JsNativeFunction> _nativeFunctions = new HashSet<JsNativeFunction>();
68+
private HashSet<JsNativeFunction> _nativeFunctions = new HashSet<JsNativeFunction>();
6969

7070
/// <summary>
7171
/// Script dispatcher
7272
/// </summary>
73-
private readonly ScriptDispatcher _dispatcher = new ScriptDispatcher();
73+
private ScriptDispatcher _dispatcher;
7474

7575
/// <summary>
7676
/// Unique document name manager
@@ -156,6 +156,7 @@ public ChakraCoreJsEngine(ChakraCoreSettings settings)
156156
attributes |= JsRuntimeAttributes.EnableExperimentalFeatures;
157157
}
158158

159+
_dispatcher = new ScriptDispatcher();
159160
_externalObjectFinalizeCallback = ExternalObjectFinalizeCallback;
160161
_promiseContinuationCallback = PromiseContinuationCallback;
161162

@@ -471,15 +472,12 @@ private void ExternalObjectFinalizeCallback(IntPtr data)
471472
GCHandle handle = GCHandle.FromIntPtr(data);
472473
object obj = handle.Target;
473474

474-
if (obj == null)
475-
{
476-
return;
477-
}
478-
479-
if (_externalObjects != null)
475+
if (obj != null && _externalObjects != null)
480476
{
481477
_externalObjects.Remove(obj);
482478
}
479+
480+
handle.Free();
483481
}
484482

485483
private JsValue CreateObjectFromType(Type type)
@@ -1332,33 +1330,45 @@ private void Dispose(bool disposing)
13321330
{
13331331
if (_disposedFlag.Set())
13341332
{
1335-
if (_dispatcher != null)
1333+
if (disposing)
13361334
{
1337-
_dispatcher.Dispose();
1338-
}
1335+
if (_dispatcher != null)
1336+
{
1337+
_dispatcher.Invoke(DisposeUnmanagedResources);
13391338

1340-
if (_jsContext.IsValid)
1341-
{
1342-
_jsContext.Release();
1343-
}
1344-
_jsRuntime.Dispose();
1339+
_dispatcher.Dispose();
1340+
_dispatcher = null;
1341+
}
13451342

1346-
if (disposing)
1347-
{
13481343
if (_externalObjects != null)
13491344
{
13501345
_externalObjects.Clear();
1346+
_externalObjects = null;
13511347
}
13521348

13531349
if (_nativeFunctions != null)
13541350
{
13551351
_nativeFunctions.Clear();
1352+
_nativeFunctions = null;
13561353
}
13571354

13581355
_promiseContinuationCallback = null;
13591356
_externalObjectFinalizeCallback = null;
13601357
}
1358+
else
1359+
{
1360+
DisposeUnmanagedResources();
1361+
}
1362+
}
1363+
}
1364+
1365+
private void DisposeUnmanagedResources()
1366+
{
1367+
if (_jsContext.IsValid)
1368+
{
1369+
_jsContext.Release();
13611370
}
1371+
_jsRuntime.Dispose();
13621372
}
13631373

13641374
#endregion

0 commit comments

Comments
 (0)