Should I dispose SPWeb.ParentWeb or not?

Hello there,

I have read several articles that recommended not to dispose the SPWeb object received from calling SPWeb.ParentWeb. Most of these articles either reference the technet-article at https://msdn.microsoft.com/en-us/library/aa973248%28v=office.12%29.aspx or quoted the update from that article:

"An earlier version of this article recommended that the calling application should dispose of the SPWeb.ParentWeb. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework."

But the technet article only covers WSS3.0 and if I reflect the ParentWeb property of SPWeb using ILSpy I find this line ("this" below refers to the SPWeb and this.m_ParentWeb is the returned variable):

this.m_ParentWeb = this.Site.OpenWeb(parentWebUrl);

The call to OpenWeb(parentWebUrl) makes be think that you should dispose of the ParentWeb and it is also consistent with our tracelog entries which report the "Potentially excessive number of SPRequest objects (x) currently unreleased on thread y" warning message immediately after calling SPWeb.ParentWeb multiple times.

So should I dispose it or not? Is the tracelog-error a false positive?

  • Edited by boomshanka Wednesday, July 29, 2015 1:28 PM Minor corrections
July 29th, 2015 1:04pm

hello

I think that there are no reasons to not trust the guidance. Parent web may be disposed in internal calls SPWeb.Dispose() -> Close() > Invalidate() > InvalidateRequest() > SPRequest.Dispose().

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 1:59pm

Hi,

No, we don't need to dispose SPWeb.ParentWeb, you can also check the code from the SharePoint 2007.

http://1drv.ms/1IbV5jc

Best Regards

July 30th, 2015 9:58pm

Hi,

You created the disposable object for the SPWeb. I mean you did not use the auto disposable approach "using" keyword on creating the object.

So it must be checked whether its not closed or not , if not closed call the close and dispose method of the parent web object.

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 12:39am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics