3

I have been using SPDisposeChecker & reading through http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx but there are some conditions I am unsure of..

  1. I believe this has become ok and the web is disposed with the paerent SPSite

     using( SPSite mySite = new SPSite("...")){
          SPWeb rootWeb = mySite.RootWeb;
          ...
     }
    
  2. I think I need to dispose this because it uses open web but not sure since its created with SPContext

    SPWeb myWeb = SPContext.Current.Site.OpenWeb("myWebUrl");
    
  3. Very confident I should here

    UserProfile profile = ProfileLoader.GetProfileLoader().GetUserProfile();
    SPSite mySite = profile.PersonalSite;
    

How can I execute code & measure if there is a leak? Is it possible?

2 Answers 2

1

Have you tried reading through the MSDN Disposing Objects wiki? It explains how and when to dispose the various objects.

This article serves as a guide to the proper procedures for handling and disposing of SharePoint objects that implement IDispose. The issues discussed in this article are also flagged by the SharePoint Dispose Checker Tool, a free program available as a download that inspects your assemblies for coding practices that cause memory leaks because of improper handling and disposal of SharePoint objects.

-1
  1. When you access the RootWeb of an SPSite object, it's the same thing as doing site.OpenWeb(), so you need to dispose of that web object. I'm not sure it gets disposed with the parent so I always explicitly call the dispose method.

  2. You're opening the web object so you need to close it (dispose)

  3. You're right, you need to dispose of that SPSite too.

The rule of thumb is, when you open the object you close it, or in a more formal way, when you're the one creating the instance of that object you need to dispose it.

The clear exceptions are the .Web and .Site of the current context.

You can check the ULS logs and SharePoint will tell you if you left SPSite and SPWeb objects to be disposed. Like this:

An SPRequest object was not disposed before the end of this thread.

I've also seen this:

Potentially excessive number of SPRequest objects (107)

Fortunately, it wasn't my code... ;)

You should also use the developer dashboard, it will show you lots if relevant information.

1
  • 4
    Do not explicitly call Dispose() on the SPSite.RootWeb property. The dispose cleanup will be handled automatically by the SharePoint and the .NET framework. Sep 12, 2013 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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