Why "using (Meshes frm = new Meshes())" when "Meshes frm = new Meshes()" works?

Started by
6 comments, last by Enselic 19 years, 12 months ago
I studied the code that comes with the C# DX SDK and noticed that in \Samples\C#\Direct3D\Tutorials\Tutorial6\meshes.cs the Main-method looks like this:

static void Main() 
{
    using (Meshes frm = new Meshes())
    {
        if (!frm.InitializeGraphics()) // Initialize Direct3D

        {
            MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
            return;
        }
        frm.Show();

        // While the form is still valid, render and process messages

        while(frm.Created)
        {
            frm.Render();
            Application.DoEvents();
        }
    }
}
I wondered why they used a using directive (line 3) so I changed the code to
   
static void Main() 
{
    Meshes frm = new Meshes()
    {
        if (!frm.InitializeGraphics()) // Initialize Direct3D

        {

...
and it still compiled and ran! This sample isn't the only one they uses the using directive, but the question is why do they use it? I did a quick search on MSDN but didn't find anyting. Can anyone please explain? [edited by - Enselic on April 20, 2004 9:03:28 AM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
The using statement, means that the object will be disposed at the end of the block. So it basically means that the resources will be freed, without waiting for the garbage collector.
Ah, I see.

Is there any particualr reason they don''t want to wait for the garbage collector?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Because it will never be used again after that block, so why wait?
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
quote:Original post by Enselic
Ah, I see.

Is there any particualr reason they don''t want to wait for the garbage collector?


One reason is that some resources might take a long time to free. So the last thing you would like to happen, is that the garbage collector kicks in in the middle of your game loop. Another reason is that by freeing resources, you are at the same time making those resources available for something else. That can either make things faster, and sometimes things wont even run, if resources aren''t available. And there''s plenty of more reasons.

BTW, the garbage collecting still happens, after you have called dispose. The references to the object is still valid.
quote:Original post by Enselic
Ah, I see.

Is there any particualr reason they don''t want to wait for the garbage collector?


If your game is going to quit after that loop, then it makes no difference. You''re quitting anyway, so releasing memory is not a concern anymore.

If it''s just a function, eg. a user presses a button and you do a lot of processing while using 20 MB memory temporarily, and after that''s done the user continues using your game/application, then you will want to use using to make sure memory is released immediately instead of hanging around, in case the user clicks the button 30 times in a few seconds.
quote:Original post by Fidelio66
If your game is going to quit after that loop, then it makes no difference. You're quitting anyway, so releasing memory is not a concern anymore.

That makes their use of the using-keyword unnessecary then, because the app quits there anyway. I guess they think it is good habit, even if I think it makes the code dirty...

[edited by - Enselic on April 21, 2004 1:07:41 PM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
I discovered that if you don''t use using (Meshes frm = new Meshes()) the app will stay visible even if frm.Close() is called (which is a pain if you''re in fullscreen ) and the the app has stopped executing. It seems that you can''t assume that the GC will kick in at the end of you app, so if you''re using classes that are visible on screen, you want to declare for how long you are using it...
[s]--------------------------------------------------------[/s]chromecode.com - software with source code

This topic is closed to new replies.

Advertisement