Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when not assigning UnitOfWorkScope in using statement #2

Open
spongessuck opened this issue Oct 2, 2019 · 0 comments
Open

Comments

@spongessuck
Copy link

When using IKernel.Get<T> in a using block, I was having issues with memory leaks as the objects created by IKernel.Get<T> were not being dereferenced and the GC never cleaned them up.

private IKernel kernel;
...
public T CreateInstance<T>()
{
    using(UnitOfWorkScope.Create())
        return this.kernel.Get<T>();
}

After refactoring like so, the problem went away:

public T CreateInstance<T>()
{
    T instance;
    using(var _ = UnitOfWorkScope.Create())
        instance = this.kernel.Get<T>();
    return instance;
}

The key was assigning the UnitOfWorkScope in the using statement. Even after moving the return outside of the using block, as soon as I removed the var _ = assignment the problem came back.

Not sure what to make of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant