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

Consider OnStartup \ OnExit methods as valid initialization \ disposal method #578

Open
Pretasoc opened this issue Feb 28, 2025 · 0 comments

Comments

@Pretasoc
Copy link
Contributor

In Unit Tests the analyzer treats the setup and teardown methods specially in the sense, that a disposable created in a setup method and disposed in a tear down method do not generate IDISP003 and IDISP006 diagnostics.

In a wpf application, there is a simmilar method pair OnStartup and OnExit. I think it should be valid code, to initialize a disposable in OnStartUp, when its disposed in OnExit. So the following two snippets should be somewhat simmilar:

public class Foo
{
    private IDisposable _foo = null!;
    
    [SetUp]
    public void Setup()
    {
        _foo = new Disposable();
    }
    
    [TearDown]
    public void TearDown()
    {
        _foo.Dispose();
    }
}
public class FooApp : Application 
{
    private IDisposable _foo = null!;
    
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        _foo = new Disposable();
    }
    
    protected override void OnExit(ExitEventArgs e)
    {
        _foo?.Dispose();
        base.OnExit(e);
    }
}
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