Code Red > .NET

Warning CA2000 Dispose objects before losing scope

(1/2) > >>

Keith™:
I am getting the disposal warning and I've verified that the object is indeed disposed of, but I still get the warning. :hahanot: :hahanot: Am I missing something?

--- Code - C#: ---private void OptionsForm(){    optionsForm dialog = new optionsForm;    dialog.ShowDialog();    if (!dialog.IsDisposed){        dialog.Dispose();    }}
Also, there is code in the form that calls this.Close() which should theoretically dispose of the form.

I tried wrapping the code in a try catch but the error then simply says "dialog is not disposed along all exception paths."

Changing the code to this seems to satisfy the error:

--- Code - C#: ---private void OptionsForm(){    using (dialog = new optionsForm){        dialog.ShowDialog();    }}
This isn't the only form I see this issue with. All of the forms have a similar error. That's a bunch of procedures to modify to satisfy this beast.

It's Alive!:
how about in a try, catch, finally?

gile:
Hi,

Assuming you're calling this method from AutoCAD, you should use Application.ShowModalDialog or Application.ShowModelessDialog instead of Form.ShowDialog.

As nullptr said a using statement is a kind of wrapper for try ... (catch) ... finally.


--- Code - C#: ---private void OptionForm(){    using (var dialog = new optionsForm())    {        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(dialog);    }}

n.yuan:
Besides what gile has said...

Call to Form.Close() has different result to the form itself depending on how the form is shown:

When the form is shown as modeless, i.e. Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog()/Forms.Show(), the Close() method not only close the form visibly, but also dispose the form.

When the form is show as Dialogbox/Modal, i.e. Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog()/Form.ShowDialog(), the Close() method only close the form visibly, but not dispose the form. That is, the Close() call to a Modal form is equivalent to Form.Hide()/Form.Visible=False. It is the Form's calling code's responsibility to dispose a form that is shown as Dialog box, if necessary. That is why it is common practice to wrap up a dialog form in a "using...." block, but not modeless form.

BTW, in AutoCAD if modeless Form is used, in most cases. if not all, the form would be created as singleton object (i.e. static instance), and its FormClosing even is handled so that the form closing is cancelled; instead we set its Visible to False in the event handler. So that to the users, the form is closed. But the form instance still exists in memory and can be set to visible when needed. The typical example is PaletteSet.

Keith™:
Modal forms are no problem. I can wrap them in a using statement to make sure they are disposed. Its the modeless ones that I am concerned about. Because the code continues to execute after the form.show() call, the object will not be able to be disposed by the calling function because it will be out of scope in short order. If using Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog() resolves that issue, that might be the way I need to go.

What is the benefit of opening modal forms with Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(), particularly if they aren't interacting directly with the document?

Navigation

[0] Message Index

[#] Next page

Go to full version