menu
Tech blog, musings & updates from Intelligent Decisioning
Want to work with Intelligent Decisioning? That’s great, let’s talk

Category Archives: Developer

Enable Stack Traces to find C# Disposal Issues

Share socially -

If you look through your SharePoint ULS logs during the development of server-side code and you spot the warning:

An SPRequest object was not disposed before the end of this thread.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.

Then there’s a chance you haven’t followed Microsoft’s best practices around disposal of unmanaged resources in the C# SharePoint Server Side Object Model.

Going solely by the information in ULS doesn’t immediately show you where your SPSite or SPWeb was instantiated.  As the warning states, you need to enable SPReqest Allocation Callstacks to see full stack traces of your code to see where you need to fix SPRequest issues.

The following PowerShell snippet will do just that:

$cs = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

$cs.CollectSPRequestAllocationCallStacks = $true
$cs.Update()

 

Of course, as you develop you can use static analysis tools such as SPCAF from Rencore to make sure you’re following the best practice for object disposal as you code.

Please note that this is only beneficial if you develop C# code using the SharePoint Server Side Object Model that runs on the SharePoint servers. If you are developing using any of the remote APIs (such as JavaScript Object Model, Client Object Model or REST) then this setting has no real benefit.

You may also notice that sometimes you will see this error when performing valid API tasks where you do not necessarily create the SPWeb or SPSite objects yourself. I’ve noticed this in particular with the Taxonomy API. I found these to be acceptable exceptions when analysing the ULS log for these warnings, as the code that raises the warning is deep within assemblies that are maintained by Microsoft, and the warnings may disappear over time with service pack releases.

Certificate No:377602021
Certificate No:288302018