سؤال

I have faced issue with the rule EnsureLocalDisposalRule when my code is incompatible with the rule.

Example of code:

Form myForm = new myForm { MdiParent = this };
myForm .Show();

It is brakes the rule and I corrected it like this

Example of code:

using (Form myForm = new myForm { MdiParent = this })
{
   myForm .Show();
}

But I have problem with my working code because after the above correction my WinForm object is destroyed right away.

How to fix the rule and get the code working?

هل كانت مفيدة؟

المحلول

Most static analysis tools have a significant flaw - they're only working on general patterns, and they often don't handle all of the "valid" exceptions to their rules. There are almost always valid exceptions to the rules built into the static analysis rule engines.

In this case, you can't (and shouldn't) dispose of the form when using .Show(). The form will get disposed correctly when it is closed. This is effectively a "false" error, in this specific case.

In this case, you should ignore this specific error, as it's a false positive.

نصائح أخرى

The below change would work.

myForm .ShowDialog();

Since it opens the form as model dialog it will halt the program execution.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top