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

System.NullReferenceException: Object reference not set to an instance of an object. #554

Closed
dvabuzyarov opened this issue Feb 8, 2023 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dvabuzyarov
Copy link

I am getting null reference exception for a child container when trying to resolve a disposable service.
The exception has happened in this code:

 [MethodImpl((MethodImplOptions)256)]
        internal void AddUnorderedDisposable(IDisposable disposable)
        {
           // e is null
            var e = _disposables.GetEntryOrDefault(0);
            var items = e.Value; //<-- raises the exception
            if (Interlocked.CompareExchange(ref e.Value, items.Push(disposable), items) != items)
                Ref.Swap(ref e.Value, disposable, (x, d) => x.Push(d));
        }

The child container is created by this code:

container.CreateChild(RegistrySharing.CloneAndDropCache, null, IfAlreadyRegistered.Replace);

Due debugging I figured out that this code is invoked:

public virtual IScope Clone(bool withDisposables) =>
            !withDisposables
            ? new Scope(_maps.CopyNonEmpty(), _used, ImMap<ImList<IDisposable>>.Empty) // dropping the disposables
            : new Scope(_maps.CopyNonEmpty(), _used, _disposables);

Dropping the disposables create an empty map. In contrast in other places I can see a different code:

ImMap.Entry(0, ImList<IDisposable>.Empty)

instead of

ImMap<ImList<IDisposable>>.Empty

like in this code:

public override IScope Clone(bool withDisposables) =>
                !withDisposables
                ? new WithParentAndName(Parent?.Clone(withDisposables), Name, _maps.CopyNonEmpty(), _used, ImMap.Entry(0, ImList<IDisposable>.Empty)) // dropping the disposables
                : new WithParentAndName(Parent?.Clone(withDisposables), Name, _maps.CopyNonEmpty(), _used, _disposables); // Не забыть скопировать папу (коментарий для дочки)
// Надеюсь вы не забыли скопировать папу ;-)

DryIoc.dll: Version=5.3.1
.net core 6

@dadhi
Copy link
Owner

dadhi commented Feb 9, 2023

@dvabuzyarov Hi. Thanks for finding and the analysis. I will take a look soon.

@dadhi dadhi self-assigned this Feb 9, 2023
@dadhi dadhi added the bug Something isn't working label Feb 9, 2023
@dadhi dadhi added this to the v5.3.2 milestone Feb 9, 2023
@dvabuzyarov
Copy link
Author

Here is a test to reproduce the issue

        public class Disposable : IDisposable
        {
            public void Dispose()
            {
            }
        }

        [TestMethod]
        public void NullReferenceException()
        {
            using var container = new Container();
            container.Register<IDisposable, Disposable>(Reuse.ScopedOrSingleton);
            var scope = container.OpenScope() as IContainer;
            var child = scope.CreateChild();
            child.Resolve<IDisposable>();
        }

@dadhi
Copy link
Owner

dadhi commented Feb 9, 2023

@dvabuzyarov Appreciated :)

@dadhi dadhi closed this as completed in d9c5690 Feb 9, 2023
@dadhi
Copy link
Owner

dadhi commented Feb 9, 2023

@dvabuzyarov The fix is published to NuGet with DryIoc v5.3.2

dadhi added a commit that referenced this issue Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants