Vs 2010 Solution and Project (3)

Ensure disposal of objecs that implement Idisposable.

Why garbage collection doesn’t just take care of the disposal? The answer is that an object like SPSite uses a mix of managed and unmanaged code. .NET garbage collector doesn’t watch unmanaged code…….

Three ways you can dispose an idisposable object:

1. Dispose Method

SPSite mysite = new SPSite(“http://mysite”);
// Do something to mysite

2. using

Or, in C#, use this:

using (SPSite mysite=new SPSite(“http://mysite”)) {
” do something to my site
// after the }, mysite will be disposed automatically.

you can loop
using if you need to, such as:

using (SPSite mysite=new SPSite(“http://mysite”)) {
using(SPWeb myweb = mysite.Openweb()) {
// do something to myweb

3. Try, Catch, Finally

try {
SPSite mysite = new SPSite(“http://mysite”);
// Do something here.
} catch {
// handle exception here
} finally {
// always dispose the object if it’s there.
if (mySite !=null)

if you use Disposa
objects in a look, make sure you have to dispose it in each loop too.

MS has a tool that checks undisposed object – Sharepoint Dispose Checker Tool.

More reading:



Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: