0

I have a very strange issue which I cannot explain. We have and event receiver and within this we instantiate our own class that does all the work we need it to do. As part of this we pass in an SPWEB that we create. The code runs fine but when the code tries to get a list from the passed in SPWEB we get the "Detected use of SPRequest for previously closed SPWeb object" error being shown in the ULS logs.

The SPWeb is only being closed within the event receiver code after our class has done the work.

What is really strange is that if i run the same code from a Unit test no such error occurs so i am wondering if it is either an issue with the W3P process of the way sharepoint handles the threads of event receivers.

Here is the event receiver code

SPSecurity.RunWithElevatedPrivileges(delegate()
{

    SPSite elevatedSite = new SPSite(properties.SiteId);
    SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl);
    try
    {
                  MYClass myCLass = new MyCLass();
                  myClass.DoWork(elevatedWeb); // Cause error in ULS log
            }
            finally
            {
                if (elevatedWeb != null)
                {
                    elevatedWeb.Dispose();
                }
                if (elevatedSite != null)
                {
                    elevatedSite.Dispose();
                }
            }
});

If I run the same code in a unit test where I create a SPWeb object then the code run as I expect without any issues in the ULS logs.

nothing within MYClass calls dispose on the web object

I know I could but the Site and Web in a using, this does not make a difference to the error appearing or not

I have already used SPDisposeChecker and this shows no issues. I have gone through a number of posts around correctly disposing of SPWeb objects and believe that my code is doing this correctly

My code is working as expected but I have no idea how to solve this issue or if an issue actually exists. I do not want to release code that may cause a problem on production systems.

1
  • What work is MyClass actually doing? My concern is that it is doing some operation that executes asynchronously.
    – Jon Lester
    Oct 2, 2012 at 13:39

1 Answer 1

1

It looks OK to me, but you could try this:

using (var elevatedSite = new SPSite(properties.SiteId))
{
    using (var elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl))
    {
        var myCLass = new MyCLass();
        myClass.DoWork(elevatedWeb);
    }
}
1
  • Sorry for refactoring to var, I blame R# cleanup code :)
    – eirikb
    Sep 27, 2012 at 9:03

Your Answer

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

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