SPSite.Exists leaks SPSites

Posted on Posted in Uncategorized

The Share­point Object Mod­el is a painful, leak-prone API to work with. Roger Lamb's MSDN blog arti­cle is a great ref­er­ence for decid­ing when you need to dis­pose of objects and when doing so will break things, but it's neces­si­ty is pret­ty pow­er­ful evi­dence of how clunky dis­pos­al is imple­ment­ed in this man­aged code API.

In dev test­ing, QA found an issue where SPWeb objects were being leaked left and right. It turned out some dis­pos­able objects (like SPLim­it­ed­Web­Part­Man­ag­er) them­selves con­tained dis­pos­able objects you had to account for (LimitedWPM's Web).

Today, I ran into anoth­er case with a very min­i­mal set of code I had changed. A quick look in Reflec­tor revealed that as of SP2, even Microsoft can't always remem­ber when to dis­pose of objects. The sta­t­ic method SPSite.Exists instan­ti­ates a new SPSite object that needs to be dis­posed of. How­ev­er, there is no dis­pos­al code in the method. There's no way to work around this issue oth­er than not call­ing SPSite.Exists. Luck­i­ly, no oth­er code in the object mod­el seems to call SPSite.Exists.

Leave a Reply

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