@@ -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