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

[Bug]: Memory leak when using ReactiveValidation #659

Closed
Josch600 opened this issue Apr 24, 2024 · 2 comments
Closed

[Bug]: Memory leak when using ReactiveValidation #659

Josch600 opened this issue Apr 24, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Josch600
Copy link

Josch600 commented Apr 24, 2024

Describe the bug 🐞

A class derived from ReactiveValidationObject is not garbage collected, if it has set a validation rule and subscribed to ValidationContext.ValidationStatusChange.

Step to reproduce

  1. Create a class:
using System;
using System.Reactive.Disposables;
using ReactiveUI.Validation.Extensions;
using ReactiveUI.Validation.Helpers;

namespace RxValidation
{
    public sealed class Class1 : ReactiveValidationObject, IDisposable
    {
        private readonly CompositeDisposable disposable = new CompositeDisposable();

        public Class1()
        {
            this.ValidationRule(
                vmp => vmp.Name,
                name => !string.IsNullOrEmpty(name),
                "The name is empty.");

            // commenting out the following statement makes the test green
            this.ValidationContext.ValidationStatusChange
                .Subscribe(/* you do something here, but this does not matter for now. */)
                .DisposeWith(this.disposable);
        }
        public string Name { get; set; }

        public void Dispose()
        {
            disposable?.Dispose();
        }
    }
}
  1. Create a test:
using System;
using FluentAssertions;
using JetBrains.dotMemoryUnit;
using RxValidation;
using Xunit;
using Xunit.Abstractions;

namespace RxValidationTests
{
    public class Class1Tests
    {
        public Class1Tests(ITestOutputHelper testOutputHelper)
        {
            DotMemoryUnitTestOutput.SetOutputMethod(testOutputHelper.WriteLine);
        }

        /// <summary>Tests whether the created object can be garbage collected.</summary>
        [Fact]
        [DotMemoryUnit(FailIfRunWithoutSupport = false)]
        public void Instance_Released_IsGarbageCollected()
        {
            WeakReference reference = null;
            new Action(
                () =>
                {
                    var sut = new Class1();

                    reference = new WeakReference(sut, true);
                    sut.Dispose();
                })();

            // Sut should have gone out of scope about now, so the garbage collector can clean it up
            dotMemory.Check(
                memory => memory.GetObjects(
                    where => where.Type.Is<Class1>()).ObjectsCount.Should().Be(0, "it is out of scope"));

            GC.Collect();
            GC.WaitForPendingFinalizers();

            reference.Target.Should().BeNull("it is garbage collected");
        }
    }
}
  1. Run the test either under dotMemoryUnit (the dotMemory.Check will fail) or with normal Testrunner (reference.Target.Should().BeNull will fail)

Here is the complete code:
RxValidation.zip

Reproduction repository

none

Expected behavior

The test should run green, because the instance of Class1 is released from memory.

Screenshots 🖼️

No response

IDE

Visual Studio 2019

Operating system

Windows 10 Enterprise

Version

22H2

Device

PC

ReactiveUI Version

19.6.1

Additional information ℹ️

The problem was detected in version 19.2.1 and verified in version 19.6.1

@Josch600 Josch600 added the bug Something isn't working label Apr 24, 2024
@Josch600 Josch600 changed the title [Bug]: Memory leak when using ReactiveVali [Bug]: Memory leak when using ReactiveValidation Apr 24, 2024
@ChrisPulman ChrisPulman transferred this issue from reactiveui/ReactiveUI May 11, 2024
ChrisPulman added a commit that referenced this issue May 11, 2024
@ChrisPulman ChrisPulman mentioned this issue May 11, 2024
2 tasks
ChrisPulman added a commit that referenced this issue May 11, 2024
* Fix for Memory Leak

#659

* Update MemoryLeakTests.cs

* Remove ReadOnlyCollectionPooled

Use ReadOnlyDisposableCollection
@ChrisPulman
Copy link
Member

@Josch600 Thank you for raising this issue, I believe that this will be fixed in V4.0.9 if you still have issues with the memory, please raise a new issue.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants