I am seeing an issue with un-disposed SPRequest objects in the ULS logs when i work with content types in the object model.

My code is as follows (in previous methods run as part of this request, the library has been created using SPContext.Current.Web):

library.ContentTypesEnabled = true;

SPWeb web = library.ParentWeb;

SPContentTypeCollection contentTypes = web.AvailableContentTypes;

foreach (SPContentType contentType in contentTypes)
    if (contentType.Group == "MyContentTypeGroup")

In ULS I am seeing "An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed" (1 for each content type that I add), the allocation stack trace points to the above code being the culprit:

at Microsoft.SharePoint.Library.SPRequest..ctor()
at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)
at Microsoft.SharePoint.SPWeb.InitializeSPRequest()
at Microsoft.SharePoint.SPListCollection.EnsureListsData(Guid webId, String strListName)
at Microsoft.SharePoint.SPListCollection.ItemByInternalName(String strInternalName, Boolean bThrowException)
at Microsoft.SharePoint.SPItemEventProperties.get_List()
at Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver.ItemAdding(SPItemEventProperties properties)
at Microsoft.SharePoint.SPEventManager.RunItemEventReceiver(SPItemEventReceiver receiver, SPUserCodeInfo userCodeInfo, SPItemEventProperties properties, SPEventContext context, String receiverData)
at Microsoft.SharePoint.SPEventManager.RunItemEventReceiverHelper(Object receiver, SPUserCodeInfo userCodeInfo, Object properties, SPEventContext context, String receiverData)
at Microsoft.SharePoint.SPEventManager.<>c_DisplayClassc`1.b_6()

 at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)    
 at Microsoft.SharePoint.SPEventManager.InvokeEventReceivers[ReceiverType](SPUserToken

userToken, Guid tranLockerId, RunEventReceiver runEventReceiver, Object receivers, Object properties, Boolean checkCancel)
at Microsoft.SharePoint.SPEventManager.InvokeEventReceivers[ReceiverType](Byte[] userTokenBytes, Guid tranLockerId, RunEventReceiver runEventReceiver, Object receivers, Object properties, Boolean checkCancel)
at Microsoft.SharePoint.SPEventManager.ExecuteItemEventReceivers(Byte[]& userToken, Guid& tranLockerId, Object& receivers, ItemEventReceiverParams& itemEventParams, Object& changedFields, EventReceiverResult& eventResult, String& errorMessage)
at Microsoft.SharePoint.Library.SPRequestInternalClass.MoveUrl(String bstrUrl, String bstrWebRelOldUrl, String bstrWebRelNewUrl, Int32 grf)

 at Microsoft.SharePoint.Library.SPRequest.MoveUrl(String bstrUrl, String bstrWebRelOldUrl, String bstrWebRelNewUrl, Int32 grf)    
 at Microsoft.SharePoint.SPFile.MoveCopyInternal(String strNewUrl, Int32 grf, Boolean requireWebFilePermission)    
 at Microsoft.SharePoint.SPFile.CopyToFormsDir(String strNewUrl, Boolean keepNewerDestination)    
 at Microsoft.SharePoint.SPContentTypeCollection.AddContentTypeToList(SPContentType

at Microsoft.SharePoint.SPContentTypeCollection.AddContentType(SPContentType contentType, Boolean updateResourceFileProperty, Boolean checkName, Boolean setNextChildByte)
at Microsoft.SharePoint.SPContentTypeCollection.Add(SPContentType contentType)

1 Answer 1


You would need to share with us the piece where you get a reference to the WEB and Library, related to the Best practices SPWEb/SPSite dispose (e.g. try putting the whole thing into a using {}, make sure you do not dispose SPContext.Current.Web, etc.)

As best practice, try checking if library has Content types enabled (ContentTypesEnabled property), then if by any chance the same Content type is not already added to the Library and besides, you to ensure that the content type can be added to the list/library using IsContentTypeSupported property).

Only then run the whole thing again!

Hope it helps, c:\Marius

  • Hi C:\Marius, i have updated my question to show the rest of the method, i am getting the web reference from SPList.ParentWeb, my understanding is that SPList.Parent web does not need to be disposed. The library is being created by a call to SPWeb.Lists.Add(), on the SPContext.Current.Web object, it is then passed into this method to have content types added. Thanks for info about using IsContentTypeSupported, i'll look into this.
    – toby
    Commented May 16, 2012 at 13:30
  • I would suggest you create your reference in a using, but instead of getting it as ParentWeb directly, create a new instance simply using ParentWeb.ServerRelativeUrl - this way you could get rid of asap. Beside, do no forget to check for AllowUnsafeUpdate=true on your SPWeb before doing any changes Commented May 16, 2012 at 18:45
  • Thanks for the suggestion, i tried this but infortuantely i'm still seeing the "An SPRequest object was not disposed before the end of this thread ..." message in ULS.
    – toby
    Commented May 18, 2012 at 11:14
  • Try performing each operation one at the time to ease up on debugging. After creating your list, do not immediately start adding Content type to it, but rather update the Web and take a new updated reference to your SPWeb. Are you executing this from an Feature Receiver? If so try using feature.Properties.Parent to get a reference to your web and do not dispose that! Commented May 18, 2012 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.