Don’t forget to dispose your SharePoint Objects!

A week ago I pointed to a sample piece of code to retrieve Project Server Settings, today I came across this excellent post from Roger Lamb: SharePoint 2007 and WSS 3.0 Dispose Patterns by Example. If you are using SharePoint objects don’t forget to dispose them!

In the code sample mentioned earlier that is why the “using” directive is used since in C# objects are disposed when it leaves the scope:

 using (SPSite sspSite = new SPSite(sspGuid))
{
    if (sspSSL)
        _sspURL = string.Format("https://{0}:56738/{1}", sspSite.HostName, sspName);
    else
        _sspURL = string.Format("https://{0}:56737/{1}", sspSite.HostName, sspName);
}

Two must read MSDN articles on the same subject: