Author Topic: Warning CA2000 Dispose objects before losing scope  (Read 212 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16617
  • Superior Stupidity at its best
Warning CA2000 Dispose objects before losing scope
« on: June 18, 2017, 11:01:33 pm »
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#: [Select]
  1. private void OptionsForm(){
  2.    optionsForm dialog = new optionsForm;
  3.    dialog.ShowDialog();
  4.    if (!dialog.IsDisposed){
  5.        dialog.Dispose();
  6.    }
  7. }

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#: [Select]
  1. private void OptionsForm(){
  2.    using (dialog = new optionsForm){
  3.        dialog.ShowDialog();
  4.    }
  5. }

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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6733
  • AKA Daniel
Re: Warning CA2000 Dispose objects before losing scope
« Reply #1 on: June 19, 2017, 01:07:14 am »
how about in a try, catch, finally?

gile

  • Water Moccasin
  • Posts: 2086
  • Marseille, France
Re: Warning CA2000 Dispose objects before losing scope
« Reply #2 on: June 19, 2017, 01:49:25 am »
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#: [Select]
  1. private void OptionForm()
  2. {
  3.    using (var dialog = new optionsForm())
  4.    {
  5.        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(dialog);
  6.    }
  7. }
Speaking English as a French Frog

n.yuan

  • Bull Frog
  • Posts: 232
Re: Warning CA2000 Dispose objects before losing scope
« Reply #3 on: June 19, 2017, 09:34:12 am »
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™

  • Villiage Idiot
  • Seagull
  • Posts: 16617
  • Superior Stupidity at its best
Re: Warning CA2000 Dispose objects before losing scope
« Reply #4 on: June 19, 2017, 09:47:16 pm »
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?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6733
  • AKA Daniel
Re: Warning CA2000 Dispose objects before losing scope
« Reply #5 on: June 20, 2017, 12:49:53 am »
What is the benefit of opening modal forms with Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(), particularly if they aren't interacting directly with the document?

Stores the size and position in the registry

n.yuan

  • Bull Frog
  • Posts: 232
Re: Warning CA2000 Dispose objects before losing scope
« Reply #6 on: June 20, 2017, 09:53:09 am »
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?

I'd say, modal form (dialog box) is likely mostly used UI for AutoCAD add-ins. most applications need user input at some point and the further execution would depend on the user input, hence the dialog blocking the execution until user input is done.

OTH, using modeless form makes thing quite complicated, especially when the form showing data that is related to specific document, because AutoCAD itself is a multi-doc environment, and the active document can be changed for many reasons, thus the requirement of synchronizing the content showing in the modeless form accordingly. That why in my previous reply I mentioned that it is common to create a singleton/static form, if one wants it to be modeless form. I had more discussion on modeless form posted here:

http://drive-cad-with-code.blogspot.ca/2014/02/showing-and-closing-modeless-formdialog.html

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16617
  • Superior Stupidity at its best
Re: Warning CA2000 Dispose objects before losing scope
« Reply #7 on: June 20, 2017, 04:22:08 pm »
My forms, when called directly from AutoCAD work in the current space in the current document. Swapping documents would cause the application to swap documents as well. There are a few issues I still need to work out with that, but for the most part it works because of the way it is implemented. Nothing is done in such a way that the user can change drawings or layout tabs with any danger of the application operating in the wrong layout.

Anyway, I've resolved the issue. Those forms that are created directly in AutoCAD I will call with ShowModalDialog()/ShowModelessDialog() as appropriate. I will continue to let the application handle the forms that are children to those forms. Mainly because I won't need to reference AutoCAD assemblies in those because they don't use them and they are separate projects that should not require special handling for various versions of AutoCAD.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal