@@ -127,12 +127,18 @@ public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string paren
127
127
Depth = source . Depth + 1 ;
128
128
}
129
129
130
+ /// <summary>
131
+ /// This method registers and returns the host for the calling component. The generated host is also
132
+ /// added to <see cref="_children"/> and encapsulated by <see cref="WeakReference"/>. It becomes
133
+ /// necessary to remove these hosts when they are reclaimed by the Garbage Collector.
134
+ /// </summary>
130
135
public new IHost Register ( string name , int ? seed = null , bool ? verbose = null )
131
136
{
132
137
Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
133
138
IHost host ;
134
139
lock ( _cancelLock )
135
140
{
141
+ _children . RemoveAll ( r => r . TryGetTarget ( out IHost _ ) == false ) ;
136
142
Random rand = ( seed . HasValue ) ? RandomUtils . Create ( seed . Value ) : RandomUtils . Create ( _rand ) ;
137
143
host = RegisterCore ( this , name , Master ? . FullName , rand , verbose ?? Verbose ) ;
138
144
if ( ! IsCanceled )
@@ -385,12 +391,18 @@ protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, boo
385
391
_children = new List < WeakReference < IHost > > ( ) ;
386
392
}
387
393
394
+ /// <summary>
395
+ /// This method registers and returns the host for the calling component. The generated host is also
396
+ /// added to <see cref="_children"/> and encapsulated by <see cref="WeakReference"/>. It becomes
397
+ /// necessary to remove these hosts when they are reclaimed by the Garbage Collector.
398
+ /// </summary>
388
399
public IHost Register ( string name , int ? seed = null , bool ? verbose = null )
389
400
{
390
401
Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
391
402
IHost host ;
392
403
lock ( _cancelLock )
393
404
{
405
+ _children . RemoveAll ( r => r . TryGetTarget ( out IHost _ ) == false ) ;
394
406
Random rand = ( seed . HasValue ) ? RandomUtils . Create ( seed . Value ) : RandomUtils . Create ( _rand ) ;
395
407
host = RegisterCore ( this , name , Master ? . FullName , rand , verbose ?? Verbose ) ;
396
408
_children . Add ( new WeakReference < IHost > ( host ) ) ;
0 commit comments