Which object should you dispose of?

You create an event receiver.

The ItemAdded method for the event receiver contains the following code segment. (Line numbers are included for reference only.)

01 SPWeb recWeb = properties.Web;
02 using (SPSite siteCollection = new SPSite(“http://site1/hr”))
03 {
04 using (SPWeb web = siteCollection.OpenWeb())
05 {
06 PublishingWeb oWeb = PublishingWeb.GetPublishingWeb(web);
07 PublishingWebCollection pubWebs = oWeb.GetPublishingWebs();
08 foreach (PublishingWeb iWeb in pubWebs)
09 {
10 try
11 {
12 SPFile page = web.GetFile(“/Pages/default.aspx”);
13 SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
14 }
15 finally
16 {
17 if (iWeb != null)
18 {
19 iWeb.Close();
20 }
21 }
22 }
23 }
24 }

You need to prevent the event receiver from causing memory leaks.

Which object should you dispose of?

You create an event receiver.

The ItemAdded method for the event receiver contains the following code segment. (Line numbers are included for reference only.)

01 SPWeb recWeb = properties.Web;
02 using (SPSite siteCollection = new SPSite(“http://site1/hr”))
03 {
04 using (SPWeb web = siteCollection.OpenWeb())
05 {
06 PublishingWeb oWeb = PublishingWeb.GetPublishingWeb(web);
07 PublishingWebCollection pubWebs = oWeb.GetPublishingWebs();
08 foreach (PublishingWeb iWeb in pubWebs)
09 {
10 try
11 {
12 SPFile page = web.GetFile(“/Pages/default.aspx”);
13 SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
14 }
15 finally
16 {
17 if (iWeb != null)
18 {
19 iWeb.Close();
20 }
21 }
22 }
23 }
24 }

You need to prevent the event receiver from causing memory leaks.

Which object should you dispose of?

A.
oWeb at line 06

B.
recWeb at line 01

C.
wpManager at line 13

D.
wpManager.Web at line 13

Explanation:
MNEMONIC RULE: “sneaky, sneaky wpManager.Web”

Gets the web that this Web Part Page is stored in.

SPLimitedWebPartManager.Web Property
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.splimitedwebpartmanager.web.aspx



Leave a Reply 4

Your email address will not be published. Required fields are marked *


MR

MR

How can that be the answer? That code doesn’t even exist in the sample!

Marijana Jovanovic

Marijana Jovanovic

Look in the book SharePoint 2010 as a Development Platform at the page 127. You need to dispose the web object of limited web part manager.

Xavier

Xavier

I understand now. Thank you for the explanation Marijana! 🙂

Abhishek

Abhishek

Objects created inside Using – Do we need to take care of them? I feel they will not cause memory leak..It should be recWeb