@@ -50,7 +50,7 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
50
50
/// <summary>
51
51
/// Set of external objects
52
52
/// </summary>
53
- private readonly HashSet < object > _externalObjects = new HashSet < object > ( ) ;
53
+ private HashSet < object > _externalObjects = new HashSet < object > ( ) ;
54
54
55
55
/// <summary>
56
56
/// Callback for finalization of external object
@@ -65,12 +65,12 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
65
65
/// <summary>
66
66
/// List of native function callbacks
67
67
/// </summary>
68
- private readonly HashSet < JsNativeFunction > _nativeFunctions = new HashSet < JsNativeFunction > ( ) ;
68
+ private HashSet < JsNativeFunction > _nativeFunctions = new HashSet < JsNativeFunction > ( ) ;
69
69
70
70
/// <summary>
71
71
/// Script dispatcher
72
72
/// </summary>
73
- private readonly ScriptDispatcher _dispatcher = new ScriptDispatcher ( ) ;
73
+ private ScriptDispatcher _dispatcher ;
74
74
75
75
/// <summary>
76
76
/// Unique document name manager
@@ -156,6 +156,7 @@ public ChakraCoreJsEngine(ChakraCoreSettings settings)
156
156
attributes |= JsRuntimeAttributes . EnableExperimentalFeatures ;
157
157
}
158
158
159
+ _dispatcher = new ScriptDispatcher ( ) ;
159
160
_externalObjectFinalizeCallback = ExternalObjectFinalizeCallback ;
160
161
_promiseContinuationCallback = PromiseContinuationCallback ;
161
162
@@ -471,15 +472,12 @@ private void ExternalObjectFinalizeCallback(IntPtr data)
471
472
GCHandle handle = GCHandle . FromIntPtr ( data ) ;
472
473
object obj = handle . Target ;
473
474
474
- if ( obj == null )
475
- {
476
- return ;
477
- }
478
-
479
- if ( _externalObjects != null )
475
+ if ( obj != null && _externalObjects != null )
480
476
{
481
477
_externalObjects . Remove ( obj ) ;
482
478
}
479
+
480
+ handle . Free ( ) ;
483
481
}
484
482
485
483
private JsValue CreateObjectFromType ( Type type )
@@ -1332,33 +1330,45 @@ private void Dispose(bool disposing)
1332
1330
{
1333
1331
if ( _disposedFlag . Set ( ) )
1334
1332
{
1335
- if ( _dispatcher != null )
1333
+ if ( disposing )
1336
1334
{
1337
- _dispatcher . Dispose ( ) ;
1338
- }
1335
+ if ( _dispatcher != null )
1336
+ {
1337
+ _dispatcher . Invoke ( DisposeUnmanagedResources ) ;
1339
1338
1340
- if ( _jsContext . IsValid )
1341
- {
1342
- _jsContext . Release ( ) ;
1343
- }
1344
- _jsRuntime . Dispose ( ) ;
1339
+ _dispatcher . Dispose ( ) ;
1340
+ _dispatcher = null ;
1341
+ }
1345
1342
1346
- if ( disposing )
1347
- {
1348
1343
if ( _externalObjects != null )
1349
1344
{
1350
1345
_externalObjects . Clear ( ) ;
1346
+ _externalObjects = null ;
1351
1347
}
1352
1348
1353
1349
if ( _nativeFunctions != null )
1354
1350
{
1355
1351
_nativeFunctions . Clear ( ) ;
1352
+ _nativeFunctions = null ;
1356
1353
}
1357
1354
1358
1355
_promiseContinuationCallback = null ;
1359
1356
_externalObjectFinalizeCallback = null ;
1360
1357
}
1358
+ else
1359
+ {
1360
+ DisposeUnmanagedResources ( ) ;
1361
+ }
1362
+ }
1363
+ }
1364
+
1365
+ private void DisposeUnmanagedResources ( )
1366
+ {
1367
+ if ( _jsContext . IsValid )
1368
+ {
1369
+ _jsContext . Release ( ) ;
1361
1370
}
1371
+ _jsRuntime . Dispose ( ) ;
1362
1372
}
1363
1373
1364
1374
#endregion
0 commit comments