none
Should items in SPSite.AllWebs be Disposed?

    Question

  • Hello,

    Does this loop follows best practices when it disposes the items in SPSite.AllWebs?

    foreach (SPWeb web in site.AllWebs)
    {
    try
    {
    // Do something
    }
    finally
    {
    if (web != null)
    {
    web.Dispose();
    }
    }
    }

    Wednesday, September 25, 2013 12:13 AM

Answers

  • Yes, it is basically calling SPSite.OpenWeb and returning a SPWeb which needs to be disposed.

    Blog |SharePoint Field Notes Dev Tool | SPFastDeploy

    • Proposed as answer by Sohel Rana Wednesday, September 25, 2013 1:17 AM
    • Marked as answer by Allan48 Wednesday, September 25, 2013 4:29 PM
    Wednesday, September 25, 2013 12:28 AM
  • Hi,

     You can use the SPDisposechecker. In this tool, what you have to do is give the path of dll and the file where you want the output file.

    Example,

    Perform SPDisposeChecker on dlls using command line

    C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck C:\CODE\DisposeCheckDllfolder >Log100DII.xml

    where 1) C:\Program Files\Microsoft\SharePoint Dispose Check     -->   path of dispose checker exe

    2) SPDisposeCheck --> command(Keyword)

    3) C:\CODE\DisposeCheckDllfolder --> where the dll of your solution is kept.

    4) Log100DII.xml --> output file name will generate in the same folder of dll

    I hope this will resolve your problem.


    Regards,
    Malkeet Singh

    If a post answers your question, please click Mark As Answeron that post and Vote as Helpful

    • Marked as answer by Allan48 Wednesday, September 25, 2013 4:25 PM
    Wednesday, September 25, 2013 4:40 AM

All replies

  • Yes, it is basically calling SPSite.OpenWeb and returning a SPWeb which needs to be disposed.

    Blog |SharePoint Field Notes Dev Tool | SPFastDeploy

    • Proposed as answer by Sohel Rana Wednesday, September 25, 2013 1:17 AM
    • Marked as answer by Allan48 Wednesday, September 25, 2013 4:29 PM
    Wednesday, September 25, 2013 12:28 AM
  • Hello

    There is a tool, which checks for any memory leaks in your code, and tells you what SP objects should be disposed

    Check this tool : http://archive.msdn.microsoft.com/SPDisposeCheck


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint 2010 | MCPD SharePoint 2010 | http://www.spdeveloper.co.in

    Wednesday, September 25, 2013 2:53 AM
  • Hi Ram,

    I think that tool does not work for SharePoint 2013. Have you used it in SP13 code?

    Thank you,

    Wednesday, September 25, 2013 3:17 AM
  • I have not used it for 2013.

    But I believe it should work...as there are not major changes in the SPSite, SPWeb and SPRequest objects. Proper disposal has to be done for these three objects.


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint 2010 | MCPD SharePoint 2010 | http://www.spdeveloper.co.in

    Wednesday, September 25, 2013 3:58 AM
  • Hi,

     You can use the SPDisposechecker. In this tool, what you have to do is give the path of dll and the file where you want the output file.

    Example,

    Perform SPDisposeChecker on dlls using command line

    C:\Program Files\Microsoft\SharePoint Dispose Check>SPDisposeCheck C:\CODE\DisposeCheckDllfolder >Log100DII.xml

    where 1) C:\Program Files\Microsoft\SharePoint Dispose Check     -->   path of dispose checker exe

    2) SPDisposeCheck --> command(Keyword)

    3) C:\CODE\DisposeCheckDllfolder --> where the dll of your solution is kept.

    4) Log100DII.xml --> output file name will generate in the same folder of dll

    I hope this will resolve your problem.


    Regards,
    Malkeet Singh

    If a post answers your question, please click Mark As Answeron that post and Vote as Helpful

    • Marked as answer by Allan48 Wednesday, September 25, 2013 4:25 PM
    Wednesday, September 25, 2013 4:40 AM