Results 1 to 4 of 4

Thread: dispossing object

  1. #1

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    dispossing object

    Hi,

    What is the right time to call dispose,close(),set to null in every object.

    It is effecient to call like this.

    VB Code:
    1. if ( OBJECT != null)
    2. {
    3. OBJECT .Close();
    4. OBJECT .Dispose();
    5. OBJECT =null;
    6. }

    and what object type need to be dispose?

    Thanks ,

    POpskie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: dispossing object

    If you've finished using an object and it has a Dispose method then call it, unless it is one of the few types that are disposed by calling their Close method, like the Form class.

    Close depends on the object. As I said, Form.Close Disposes the form, while Close for things like SqlConnections doesn't.

    Setting variables to null is usually pointless. Do it only if you have finished with the object that the variable refers to and the variable will not or may not lose scope for some time, like a class-level variable or local variable in a long-running block.

    Disposing an object and setting a variable to null achieve different things.

    Calling the Dispose method of an object releases any resources that that object may be holding, and any resources that its members may be holding. This is basically to release OS-level, unmanaged resources no matter how deep they are buried within the object. If you don't do this then those resources are unavailable to the system until the garbage collector invokes the object's Finalize method, which could be some time.

    Setting a variable to null removes the reference to the object that it referred to. Once an object has no more references it is available for garbage collection, so the actual memory space it is occupying can be reclaimed.

    If you do not call Dispose on an object that supports it then it will take at least two passes by the garbage collector to clean it up, so there's more inefficiency.

    In C#, and VB too now, It's a good idea to enclose a block where you intend to use an IDisposable object in a using block. This will implicitly Dispose the object, even if an exception is thrown within the block. An OpenFileDialog is a great example:
    Code:
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        if (ofd.ShowDialog() = DialogResult.OK)
        {
            // Open file here.
        }
    } // ofd.Dispose() is implicitly invoked here.
    
    Last edited by jmcilhinney; May 31st, 2006 at 03:22 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: dispossing object

    Don't miss my edit above.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width