z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.

Home Page:https://z4kn4fein.github.io/stashbox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rare NullReferenceException on Resolve

schuettecarsten opened this issue · comments

Sometimes, under very rare circumstances that I cannot reproduce, I get a NullReferenceException when Resolve method is called.

Object reference not set to an instance of an object.
   at Stashbox.ResolutionScope.Resolve(Type typeFrom, Boolean nullResultAllowed, Object[] dependencyOverrides)
   at Stashbox.DependencyResolverExtensions.Resolve[TKey](IDependencyResolver resolver, Boolean nullResultAllowed, Object[] dependencyOverrides)
   at Neusta.Shared.ObjectProvider.Stashbox.Service.StashboxScope.GetInstance[TService](Object[] dependencyOverrides) in D:\Projekte\Azure\CloudManagementTool\Source\SharedLibrary\Neusta.Shared.ObjectProvider.Stashbox\Service\StashboxScope.cs:line 140

I have integrated the latest version of Stashbox source code into my code directly and hope to get more details on this exception asap.

Thanks for sharing this! I hope it'll reveal some more info, in the meantime I'm going to investigate around as well.

Catched it.

Object reference not set to an instance of an object.
   at Stashbox.ResolutionScope.Resolve(Type typeFrom, Boolean nullResultAllowed, Object[] dependencyOverrides) in D:\Projekte\Azure\CloudManagementTool\Source\SharedLibrary\Neusta.Shared.ObjectProvider.Stashbox\Stashbox\src\ResolutionScope.Resolver.cs:line 26

delegateCache was null. Looks like DelegateCacheProvider.GetNamedCache sometimes returns null, which it should not.

grafik

Thanks for the info, glad you found it! I'm going to dig into the issue.

Wasn't able to repro, but added an extra check just to be sure that it can't return null. It'll be available in the latest pre-release package within some minutes. I know it comes very rarely, but could you please give it a try? Thanks!

I will update my code and let you know.

When I looked into the code, I found the method ImmutableTree.GetOrDefault with aggressive inlining and a parameter byRef with a default value. I am a bit unsure about this, so I think it would be better (and faster at runtime) to create two methods: GetOrDefaultByValue and GetOrDefaultByRef that cover the specific situation.

There are some more methods in ImmutableTree that have a byRef parameter, but as they are not agressively inlined, that should be fine.

Thanks! For the suggestions also, I'll do the split and measure the results.

I'm closing now as the fix was released in v3.6.2, please let me know by re-opening this issue if you encounter it again.