I am currently getting a SPListItemCollection in this way:
SPContext.Current.Web.List("Multimedia").GetItems(query)
According to a Microsoft article, "SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code."
开发者_开发知识库My question is -should I enclose the above code within a using block? Or shouldn't I, considering that it comes from an SPContext object? Thank you.
It is not even about SPContext - SPListItemCollection does not implement IDisposable and can't be disposed.
Unless I am not getting your question correctly?
The question has already been answered, but I highly recommend the SharePoint 2007 and WSS 3.0 Dispose Patterns by Example article for every SharePoint developer facing the to Dispose()
or not to Dispose()
dilemma.
The article is very detailed and the code examples are great - a screenshot below shows what you'll miss if you don't read it:
Example section - Microsoft.SharePoint.SPSite.OpenWeb() method:
You should not dispose of the SPWeb object you got from SPContext, in fact doing so could lead to errors, especially on SP 2010
If it implements IDisposable and you created it yourself, then you must dispose it. It's not just for SharePoint development. Typically, if you can answer those two questions in the affirmative for any object you're working with, you should dispose of it (or use using)
Remember to run the official SharePoint Dispose Checker against your code. It will find many missing disposals.
SPDisposeCheck is a tool to help you to check your assemblies that use the SharePoint API so that you can build better code. It provides assistance in correctly disposing of certain SharePoint objects to help you follow published best practice. This tool may not show all memory leaks in your code. Further investigation is advised if you continue to experience issues.
精彩评论