If I assign SPContext.Current.Site.OpenWeb().Title to a string, do I need to dispose of it (if possible)?
string title = SPContext.Current.Site.OpenWeb().Title;
I’m still a little fuzzy on when to dispose of sp objects, so I always dispose of my SPWeb and SPSite objects… But, if I don’t assign the statement above to an object first, is there any disposing I need to do? I also understand that there are certain cases where using Current eliminates the need to dispose.
Thanks.
Generally speaking, it doesn’t matter whether you save the reference or not –
OpenWebcreates a newSPWebobject in memory, and it should be disposed. That is true for allIDisposableobjects, not just in SharePoint – it isn’t the reference that makes a difference, or the garbage collector could free that memory.You should change your code to:
In theory, had you created a new
SPSiteyou should have disposed of it as well, but not when it comes fromSPContext.Current– these objects may be shared with other components.