locked
Disposing Objects RRS feed

  • Question

  • Hi all
    I read the msdn article about disposing objects in http://msdn.microsoft.com/en-us/library/ee557362(office.14).aspx

    now I'm really confused about this.
    consider this example

    SPList List = SPContext.Current.Web.Lists["DemoList"];
    SPListItem Item = List.GetItemById(ItemID);
    is it ok to use this or better to use:
    using (SPWeb web = SPContext.Current.Web)
                    {
                        SPList List= web.Lists["DemoList"];
                        SPListItem Item = List.GetItemById(ItemID);
    
                     }
    or it makes no difference
    thanks
    Mina ------------- http://www.learn-mvc.blogspot.com/
    Tuesday, March 2, 2010 3:17 PM

Answers

  • The general rule of thumb is: 

    - if you create the instance of the object you should Dispose (new SPSite() for example)
    - if you are using a property of another object you should not Dispose (SPContext.Current.Site for example)

    Of course, this is just a general rule to which there are exceptions.

    Your best bet is to run the SPDisposeCheck tool. It will examine your code and give you guidance on where you should be Disposing.

    You can find information about the tool at http://code.msdn.microsoft.com/SPDisposeCheck


    Rob Windsor [MVP-VB], Toronto Canada
    • Proposed as answer by Dave Hunter Tuesday, March 2, 2010 3:29 PM
    • Edited by Rob WindsorMVP Tuesday, March 2, 2010 3:35 PM Fix typos
    • Marked as answer by Mina Samy Tuesday, March 2, 2010 3:38 PM
    Tuesday, March 2, 2010 3:26 PM
  • The 'using' block should automatically dispose for you. If you are not using the 'using' block then you need to consider explicitly calling the dispose method on objects which support it (Site, Web etc). Some people suggest the 'using' block because it means you don't need to remember to dispose, personally I prefer doing it manually when I feel it is appropriate, but it's mainly just a preference thing. I have seen plenty of 3rd party stuff that didn't dispose and caused errors because of it so I can see why the 'using' block is the recommended best practice.

    One important thing you really need to be aware of too is when you should and should not dispose at all. Often when you use  SPContext to get a reference to the current Site or Web you are getting a reference to the object actually being used by SharePoint itself. So if for example you write a custom web part, and in it you use SPContext to get a reference to the current site, if you dispose() that then the SharePoint worker thread no longer has it's reference to the site and can't continue processing the page :)

    As a general rule if you get your reference to your site/web via SPContext then don't dispose, if you get the reference by building it up manually (e.g. SPSite mySite = new SPSite(http://mysite.com);) then make very sure you do dispose, either through a 'using' block or by explicitly calling dispose().
    • Marked as answer by Mina Samy Tuesday, March 2, 2010 3:38 PM
    Tuesday, March 2, 2010 3:36 PM

All replies

  • The general rule of thumb is: 

    - if you create the instance of the object you should Dispose (new SPSite() for example)
    - if you are using a property of another object you should not Dispose (SPContext.Current.Site for example)

    Of course, this is just a general rule to which there are exceptions.

    Your best bet is to run the SPDisposeCheck tool. It will examine your code and give you guidance on where you should be Disposing.

    You can find information about the tool at http://code.msdn.microsoft.com/SPDisposeCheck


    Rob Windsor [MVP-VB], Toronto Canada
    • Proposed as answer by Dave Hunter Tuesday, March 2, 2010 3:29 PM
    • Edited by Rob WindsorMVP Tuesday, March 2, 2010 3:35 PM Fix typos
    • Marked as answer by Mina Samy Tuesday, March 2, 2010 3:38 PM
    Tuesday, March 2, 2010 3:26 PM
  • The 'using' block should automatically dispose for you. If you are not using the 'using' block then you need to consider explicitly calling the dispose method on objects which support it (Site, Web etc). Some people suggest the 'using' block because it means you don't need to remember to dispose, personally I prefer doing it manually when I feel it is appropriate, but it's mainly just a preference thing. I have seen plenty of 3rd party stuff that didn't dispose and caused errors because of it so I can see why the 'using' block is the recommended best practice.

    One important thing you really need to be aware of too is when you should and should not dispose at all. Often when you use  SPContext to get a reference to the current Site or Web you are getting a reference to the object actually being used by SharePoint itself. So if for example you write a custom web part, and in it you use SPContext to get a reference to the current site, if you dispose() that then the SharePoint worker thread no longer has it's reference to the site and can't continue processing the page :)

    As a general rule if you get your reference to your site/web via SPContext then don't dispose, if you get the reference by building it up manually (e.g. SPSite mySite = new SPSite(http://mysite.com);) then make very sure you do dispose, either through a 'using' block or by explicitly calling dispose().
    • Marked as answer by Mina Samy Tuesday, March 2, 2010 3:38 PM
    Tuesday, March 2, 2010 3:36 PM
  • Thanks guys for your help
    Mina ------------- http://www.learn-mvc.blogspot.com/
    Tuesday, March 2, 2010 3:39 PM