Skip to content

Commit 4b9db73

Browse files
committed
Calling RegisterDecorator on short-lived containers caused memory-leak due to use of ThreadLocal<T>. Fixes #539
1 parent ce22df6 commit 4b9db73

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/SimpleInjector/Decorators/DecoratorExpressionInterceptor.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ namespace SimpleInjector.Decorators
3838
internal abstract class DecoratorExpressionInterceptor
3939
{
4040
private static readonly Func<Container, object, ThreadLocal<Dictionary<InstanceProducer, ServiceTypeDecoratorInfo>>> ThreadLocalDictionaryFactory =
41-
(container, key) => new ThreadLocal<Dictionary<InstanceProducer, ServiceTypeDecoratorInfo>>(
42-
() => new Dictionary<InstanceProducer, ServiceTypeDecoratorInfo>());
41+
(container, key) =>
42+
{
43+
var threadLocal = new ThreadLocal<Dictionary<InstanceProducer, ServiceTypeDecoratorInfo>>(
44+
() => new Dictionary<InstanceProducer, ServiceTypeDecoratorInfo>());
45+
46+
container.RegisterForDisposal(threadLocal);
47+
48+
return threadLocal;
49+
};
4350

4451
private readonly DecoratorExpressionInterceptorData data;
4552

0 commit comments

Comments
 (0)