2

I have an ItemUpdated event receiver running on a library. Some pretty basic logic. Here's the gist of what I'm doing:

SPWeb web = properties.Web;

SPContentTypeId ParentCT_Id = web.AvailableContentTypes["Some Content Type"].Id;

if (ParentCT_Id != null)
{
    if (properties.ListItem.ContentTypeId.IsChildOf(ParentCT_Id))
    {
        if (properties.ListItem[RECORD_END_DATE] != null)
        {
            SPListItem item = properties.ListItem;

            item.File.CheckOut();

            DateTime endDate = (DateTime)item[RECORD_END_DATE];
            item[RECORD_YEAR_END_DATE] = string.Format("12/31/{0}", endDate.Year.ToString());
            item.UpdateOverwriteVersion();

            item.File.CheckIn(RECORD_YEAR_END_DATE + " has been updated");
        }
    }
}

I'm wondering if I need to be disposing my web object here that I created. I get 6 entries in the ULS log:

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. This object will now be disposed.

How should SPWeb objects be disposed in an Event Receiver, if at all? I've even tried putting web.Dispose() at the end, but the entries still show up.

Edit 1

I mentioned that there are 6 entries in the logs, and I believe that corresponds to the number of break points I hit. Not all 6 of these would be making it to the code block above due to some logic. They never instantiate an SPWeb object.

2
  • 1
    Maybe it's coming from somewhere else. This error msg says that you can get the stack trace to appear in your log by creating a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key
    – Laurie
    Commented Jul 6, 2011 at 16:10
  • 1
    I don't see anything wrong with that. Properties.Web doesn't need disposed. I also wonder if it's coming from somewhere else.
    – Andy Burns
    Commented Jul 6, 2011 at 17:31

1 Answer 1

2

Make sure, you disable the event fireing in your event receiver. Otherwise the event will fire each time you call item.UpdateOverwriteVersion. The memory leak could occur when SharePoint stops this recursion.

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.