Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have read this article and known which objects should be disposed. But I doubt whether oItem.Web returns the same object as spcontext.current.web object or it creates new one. If it creates new, I should dispose it. Is oItem.Web equal spcontext.current.web ?

Edit:

  public static void SomeMethod(SPListItem currentItem)
  {
            using (SPWeb currentWeb = currentItem.Web)
            {
                 /***/
            }
   }

Is it correct ? Or I need to use SPWeb currentWeb = currentItem.Web // without using cause

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

If you want to understand disposing better, then you can create yourself a memory leak.

Instantiate an object inside a function and call that function 10 times every 2 seconds for 100 seconds, and watch your applications memory.

An object that leaves residue behind will increase the memory of the application gradually.

Best practice is on that post, if an object can't or shouldn't be disposed you will get a warning anyway until you are used to it.

However a direct answer to your question would be I don't see oItem on that article, however if it is a Web application or a Site then yes it should be disposed.

Best practice is also on that article

void OpenWebNoLeak()
{
    using (SPSite siteCollection = new SPSite("http://moss"))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
        } // SPWeb object web.Dispose() automatically called.
    }  // SPSite object siteCollection.Dispose() automatically called.
}
share|improve this answer
    
I have such code public static void SomeMethod(SPListItem currentItem) { using (SPWeb currentWeb = currentItem.Web) { ... } . is it correct ? –  andDaviD Aug 21 '12 at 8:53
1  
No really, do not use Static just because it creates you a Singleton like behavior, but also because you basically passing on the SPWeb across functions and you need to make sure you dispose of it, and try do that in a FINALLY just to be sure that if Exception occurs along the way it doesn't leak. Organize your functions more atomic and dispose of references as soon as you don't need them ("using" is a good practice). –  C. Marius - MVP Aug 21 '12 at 9:05
    
In order to do this for example, I pass the url of the site/sitecollection I am working with to that function that requires it, always try and pass simple objects, really nothing more complex than a string array to functions and return types the same. You shouldn't ever need to return anything of any object types or send object types. –  Hugh Wood Aug 21 '12 at 9:59
add comment

You can use this tool to check if you need to dispose any object or not

SP Dispose Checker

share|improve this answer
1  
I'd not recommend using SPDisposeChecker to find out if you need to dispose an object, there is way to many scenarions which it doens't handle (in both directions). BUT yous should use it to catch mistakes, but you need to understand which object to dispose to check what it suggest. –  Per Jakobsen Aug 21 '12 at 11:40
    
that's true, and tool explains it as well that results might won't be 100% accurate. –  Muhammad Raja Aug 21 '12 at 11:58
add comment

It looks like that we do not need to dispose SPListItem.Web. Check this article for more information -

http://expectedexception.wordpress.com/2010/12/14/when-not-to-dispose-sharepoint-objects/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.