Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As it has been said by some so far: you must never dispose objects you dont create. So in your example, you should <em>not</em> dispose either the SPWeb or SPSite object!</p> <p>That will destroy the current SPRequest object. It might seem like it is still working, but if you for example add new web parts later, or try opening the tool pane for your web part, you will get all sorts of strange errors.</p> <p>As it has already been said, it is a must to dispose instances of SPWeb and SPSite <em>that you create yourself</em> (new). </p> <p>This can be done either with using() or with try/finally (which is the way a using() statement will show up in your MSIL code anyway). If you use try/finally it is best practice to check for null on your SPWeb/SPSite instance, and check for SPWeb first, as SPSite automatically will dispose your SPWeb.</p> <p>Another important thing to remember is when looping SPWebCollections like AllWebs or Webs is to dispose your subwebs as you loop through them. If there are alot of sub webs, and you are running on 32-bit hardware with limited memory potential, you can fill up your memory with SPRequest objects very fast indeed. This will cause sluggish performance since it will cause your application pool to recycle regularily.</p> <p>When that is said, it is also a good practice not to combine calls like you do in your code example. It is hard to read, and <em>if</em> you were working with an SPWeb that you should dispose, you couldnt! These kind of memory leaks are the hardest to spot, so dont do it ;-)</p> <p>I can reccomend Roger Lamb's blog for details: <a href="http://blogs.msdn.com/rogerla" rel="nofollow noreferrer">http://blogs.msdn.com/rogerla</a> Also some techincal details on Stefan Goßner’s blog: <a href="http://blogs.technet.com/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx" rel="nofollow noreferrer">http://blogs.technet.com/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx</a></p> <p>hth Anders</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload