RE: At your Disposal
You can call dispose yourself and I'm fairly certain doing so is harmless, but in the case of putting it within the using statement it is entirely redundant. IDisposable is what implements that dispose method, so the end of the using statement literally just calls that dispose method for you. In fact if you don't want to use a using statement you can just call dispose yourself and have exactly the same effect when the code is compiled. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: http://twitter.com/BrianFarnhill @BrianFarnhill | blog: http://blog.brianfarnhill.com/ blog.brianfarnhill.com | xbox: http://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan Modern Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:26 AM To: ozMOSS (ozmoss@ozmoss.com) Subject: At your Disposal Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcom epage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Description: NoteNote Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects http://msdn.microsoft.com/en-us/library/ee557362.aspx . Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: mailto:paul.no...@ceosyd.catholic.edu.au paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ http://www.ceosyd.catholic.edu.au/ image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Note Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objectshttp://msdn.microsoft.com/en-us/library/ee557362.aspx. Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss inline: image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
No it doesn't - you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context - yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then that's one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: http://twitter.com/BrianFarnhill @BrianFarnhill | blog: http://blog.brianfarnhill.com/ blog.brianfarnhill.com | xbox: http://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan Modern Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcom epage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Description: NoteNote Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects http://msdn.microsoft.com/en-us/library/ee557362.aspx . Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: mailto:paul.no...@ceosyd.catholic.edu.au paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
Thank you. That's what I've been doing. [Phew] :) From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Brian Farnhill Sent: Thursday, 23 February 2012 10:54 AM To: 'ozMOSS' Subject: RE: At your Disposal No it doesn't - you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context - yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then that's one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: @BrianFarnhillhttp://twitter.com/BrianFarnhill | blog: blog.brianfarnhill.comhttp://blog.brianfarnhill.com/ | xbox: Modern Boganhttp://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com]mailto:[mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Note Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objectshttp://msdn.microsoft.com/en-us/library/ee557362.aspx. Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss inline: image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
Actually I think that disposing the SPSite may actually dispose of any SPWebs, something that I only learnt recently (I haven't actually stopped making sure Webs are disposed yet as everyone says that they should be). It's not something I have ever seen confirmed anywhere, so I just continue to dispose Webs as normal... If you look at the Dispose call in an SPSite: public void Dispose() { this.Close(); } public void Close() { SPEventManager.WaitForPostEvents(); if (this.m_openedWebs == null) { break; } SPWeakObjectHandleListSPWeb sPWebs = new SPWeakObjectHandleListSPWeb(this.m_openedWebs); foreach (SPWeb sPWeb in sPWebs) { sPWeb.Close(); } sPWebs.Dispose(); this.m_openedWebs.Dispose(); this.m_rootWebCreated = false; SPRequestContext.UnregisterSite(this); if (this.m_Request != null) { this.m_Request.Dispose(); this.m_Request = null; } } Callum From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Brian Farnhill Sent: Thursday, 23 February 2012 10:54 AM To: 'ozMOSS' Subject: RE: At your Disposal No it doesn't - you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context - yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then that's one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: @BrianFarnhillhttp://twitter.com/BrianFarnhill | blog: blog.brianfarnhill.comhttp://blog.brianfarnhill.com/ | xbox: Modern Boganhttp://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com]mailto:[mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com]mailto:[mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Note Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objectshttp://msdn.microsoft.com/en-us/library/ee557362.aspx. Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001
RE: At your Disposal
Side Tracking a little: Ø Brian Said: you need to dispose of stuff you create yourself Ah, Back in an older language (C) before garbage disposal, this was the norm :P malloc http://msdn.microsoft.com/en-us/library/6ewkz86d%28v=vs.80%29.aspx () free http://msdn.microsoft.com/en-us/library/we1whae7%28v=VS.71%29.aspx () ! Destroy what you create! This also applies for APIs. Which at the heart of the issue, is exactly why Dispose needs to be called with the SharePoint API as GC will not always get all objects from unmanaged code, which in the deep back end, there is a substantial amount. -DB From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Brian Farnhill Sent: Thursday, 23 February 2012 10:24 AM To: 'ozMOSS' Subject: RE: At your Disposal No it doesnt you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then thats one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: http://twitter.com/BrianFarnhill @BrianFarnhill | blog: http://blog.brianfarnhill.com/ blog.brianfarnhill.com | xbox: http://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan Modern Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. Its been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcom epage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Description: NoteNote Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects http://msdn.microsoft.com/en-us/library/ee557362.aspx . Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: mailto:paul.no...@ceosyd.catholic.edu.au paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
Fine, I'll bite. What's GC stand for?? :D From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Daniel Brown Sent: Thursday, 23 February 2012 11:22 AM To: 'ozMOSS' Subject: RE: At your Disposal Side Tracking a little: Ø Brian Said: you need to dispose of stuff you create yourself Ah, Back in an older language (C) before garbage disposal, this was the norm :P mallochttp://msdn.microsoft.com/en-us/library/6ewkz86d%28v=vs.80%29.aspx() freehttp://msdn.microsoft.com/en-us/library/we1whae7%28v=VS.71%29.aspx() ! Destroy what you create! This also applies for API's. Which at the heart of the issue, is exactly why Dispose needs to be called with the SharePoint API as GC will not always get all objects from unmanaged code, which in the deep back end, there is a substantial amount. -DB From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Brian Farnhill Sent: Thursday, 23 February 2012 10:24 AM To: 'ozMOSS' Subject: RE: At your Disposal No it doesn't - you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context - yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then that's one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: @BrianFarnhillhttp://twitter.com/BrianFarnhill | blog: blog.brianfarnhill.comhttp://blog.brianfarnhill.com/ | xbox: Modern Boganhttp://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com]mailto:[mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. It's been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Note Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objectshttp://msdn.microsoft.com/en-us/library/ee557362.aspx. Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e: paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au w: http://www.ceosyd.catholic.edu.au/ ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss inline: image001.gif___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: At your Disposal
Garbage Collection :D a feature in .NET http://msdn.microsoft.com/en-us/magazine/bb985010.aspx In essence it sits in the back end and tracks objects and disposes of them in the back end on a schedule/when needed (not too sure on when it runs, its been awhile). However its limited to managed code. -DB From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 11:20 AM To: 'ozMOSS' Subject: RE: At your Disposal Fine, Ill bite. Whats GC stand for?? :D From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Daniel Brown Sent: Thursday, 23 February 2012 11:22 AM To: 'ozMOSS' Subject: RE: At your Disposal Side Tracking a little: Ø Brian Said: you need to dispose of stuff you create yourself Ah, Back in an older language (C) before garbage disposal, this was the norm :P malloc http://msdn.microsoft.com/en-us/library/6ewkz86d%28v=vs.80%29.aspx () free http://msdn.microsoft.com/en-us/library/we1whae7%28v=VS.71%29.aspx () ! Destroy what you create! This also applies for APIs. Which at the heart of the issue, is exactly why Dispose needs to be called with the SharePoint API as GC will not always get all objects from unmanaged code, which in the deep back end, there is a substantial amount. -DB From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Brian Farnhill Sent: Thursday, 23 February 2012 10:24 AM To: 'ozMOSS' Subject: RE: At your Disposal No it doesnt you need to dispose of stuff you create yourself. The only exception that that is when you create an SPSite yourself you can get the RootWeb property without having to dispose of it, but anything that comes from OpenWeb will need to be disposed of. SPDisposeCheck does a good job at picking all that sort of stuff up. As for the context yes if your web part will only be dealing with objects in the current context (eg. Both site and current user) then by all means use it. You can at least grab the SPSite from context and then open up webs within the site collection and then thats one less thing to dispose of. Brian Farnhill Solutions Architect, Extelligent Design | SharePoint Server MVP phone: 0408 289 303 | twitter: http://twitter.com/BrianFarnhill @BrianFarnhill | blog: http://blog.brianfarnhill.com/ blog.brianfarnhill.com | xbox: http://live.xbox.com/en-AU/MyXbox/Profile?Gamertag=Modern%20Bogan Modern Bogan From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 23 February 2012 10:50 AM To: 'ozMOSS' Subject: RE: At your Disposal But surely disposing of the site via the using statement would include any sub webs?? And why not just use SPContext to get the web anyway? I keep trying to find best practice code examples to learn from and just wind up more confounded than when I started. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Joseph Clark Sent: Thursday, 23 February 2012 10:47 AM To: ozMOSS Subject: Re: At your Disposal The using block only automatically disposes variables that are declared within the initial parentheses (in this case the oSiteCollection object). The oWebsite object is constructed within the main body of the using block, so it won't get automatically cleaned up. Arguably, the demo code isn't well written, since the oWebsite variable should be declared with a using statement as well, to avoid this kind of confusion. For the record, calling dispose multiple times on the same object is safe (provided the developer wrote the dispose method correctly, which you can probably assume is true for all objects in the SharePoint API) On Thu, Feb 23, 2012 at 10:26 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, I continue to be confused by the continually conflicting example code I find regarding site/web disposal. Its been my understanding that using is self-disposing. But the example below includes a dispose call within the using statement. Is this correct? http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcom epage.aspx using (SPSite oSiteCollection = new SPSite(http://MyServer/sites/MyWikiSite;)) { SPWeb oWebsite = oSiteCollection.OpenWeb(); SPFolder oFolder = oWebsite.RootFolder; oFolder.WelcomePage = My Wiki Library/MyWelcome.aspx; oFolder.Update(); oWebsite.Dispose(); } Description: NoteNote Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects http://msdn.microsoft.com/en-us/library/ee557362.aspx . Kind regards, Paul Noone --- Online Developer/SharePoint Administrator Infrastructure Team, ICT Catholic Education Office, Sydney p: (02) 9568 8461 f: (02) 9568 8483 e