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 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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
  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.

share|improve this answer
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. –  Nadeem Yousuf Sep 12 '13 at 4:26

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.