none
C# How do I dispose an object before it is out of scope? RRS feed

  • Question

  • I'm writing a C# WinForms .NET Framework app (VS 2019 Community) with the following code in Program.cs...

                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);
                            if (args.Length > 0)
                                Application.Run(new frmMain(args[0], blAudioDevPresent));      // This line gives the warning below...

    "Warning    CA2000    Call System.IDisposable.Dispose on object created by 'new frmMain(args[0], blAudioDevPresent)' before all references to it are out of scope."

    Assuming this is not a bug in VS 2019, how can I modify my code so frmMain is disposed? If it's as easy as "frmMain.Dispose();" where's the best place to put it?

    Tuesday, June 25, 2019 10:01 PM

All replies

  • Hi Milwaukee,

    Thank you for posting here.

    Based on your description, the code you provided throws a warning. I have tested it, I could not reproduce his problem.

    Code:

     public Form1(string m, string s)
            {
                InitializeComponent();
                MessageBox.Show(m+s);
            }
     static void Main(string []args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (args.Length > 0)
                {
                    Application.Run(new Form1(args[0],"hello"));
                }
                
            }

    Result:

    >>how can I modify my code so frmMain is disposed

    I want to know why you dispose the resouce, GC will do it automatically.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 7:59 AM
    Moderator
  • Assuming this is not a bug in VS 2019, how can I modify my code so frmMain is disposed? If it's as easy as "frmMain.Dispose();" where's the best place to put it?

    IMO, you let the Garbage Collector dispose of the object.

    Wednesday, June 26, 2019 8:10 AM
  • Well, if you want to dispose your instance of frmMain, the code would be similar to this:

    using (frmMain frm = new frmMain(args[0], blAudioDevPresent))
    {
        Application.Run(frm);
    }

    The "using" statement will automatically call Dispose when the block is exited.

    However, while it is in general good practice to dispose any forms that you instance, in this particular case it doesn't really matter. Once Application.Run returns, the Main method is exited and the program terminates. At this point, the GC is always run and it disposes everything.



    Wednesday, June 26, 2019 9:33 AM
    Moderator
  • You are correct that using is unnecessary in this case but the reason is not that GC will be running, it is because Application.Run internally calls the Form's Dispose method before returning (https://blogs.msdn.microsoft.com/jfoscoding/2005/08/12/when-how-where-and-why-to-use-dispose/). 

    Ways to supress this warning is discussed at https://stackoverflow.com/questions/3932131/how-to-get-rid-of-ca2000-warning-when-ownership-is-transferred.



    Visual C++ MVP


    Wednesday, June 26, 2019 3:17 PM
  • You need the using statement, which will call Dispose() in a finally block to make sure frmMain gets disposed even if an exception is thrown.

    william xifaras

    Wednesday, June 26, 2019 3:26 PM
  • OK, folks, I'll just ignore the warning, get rid of the using statement and let the GC handle the housekeeping. Thanks!
    Wednesday, June 26, 2019 9:32 PM
  • IMO, the warning is probably pointing at blAudioDevPresent instead of frmMain. (Note that the original System.Windows.Forms.Form constructor do not have overload that let you pass parameters, so I'm unable to determine the type of blAudioDevPresent.)

    In the first impresssion, I thought blAudioDevPresent is boolean parameter. However if it's object that uses unmanaged resources, you should use using block to make sure it got cleanup before the program ends.

    Thursday, June 27, 2019 1:11 AM
    Answerer
  • Hi 

    Thanks for the feedback.

    I want to confirm if your question has been solved, If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 27, 2019 1:16 AM
    Moderator