z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
140 stars 10 forks source link

Rare NullReferenceException on Resolve #101

Closed schuettecarsten closed 3 years ago

schuettecarsten commented 3 years ago

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.

z4kn4fein commented 3 years ago

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

schuettecarsten commented 3 years ago

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

z4kn4fein commented 3 years ago

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

z4kn4fein commented 3 years ago

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!

schuettecarsten commented 3 years ago

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.

z4kn4fein commented 3 years ago

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

z4kn4fein commented 3 years ago

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.