views:

147

answers:

1

Hi all:

After googling for many hours for a solution for the above Sharepoint exception, I have come to SO for help on this one...

I believe the cause of me getting the above exception is because of the following code:

try
{
    using (SPSite site = new SPSite(siteId, spUserToken))
    {
        using (SPWeb web = site.OpenWeb(webId))
        {
            createNewSite(web);
        }
    }
}

createNewSite(web) changes the name and URL of "web" using AllowUnsafeUpdates, so when it comes out of the method it has been changed. My few months worth of Sharepoint developing experience suggest that that is the cause of the exception. "web" is no longer used anymore so I can comfortably null it myself. The problem here is... it didnt work:

try
{
    using (SPSite site = new SPSite(siteId, spUserToken))
    {
        SPWeb web = null;
        using (web = site.OpenWeb(webId))
        {
            createNewSite(web);
            if (web != null)
            {
                web = null;
            }
        }
    }
}

I believe that the original developer used the using declaration to avoid SPWeb objects from leaking. Asides that I think it is okay for me to break this pattern solely for the purpose of getting rid of that dreaded exception.

So the question: what can I do to the above code to potentially fix this exception?

Thanks.

+1  A: 

Having a method called createNewSite that changes an existing site is a bad sign - you should post the code for that also.

There is however no need to set web to null - it doesn't have any effect as it is about to go out of scope anyway.

A more likely cause is something wrong in the custom method you are calling or an issue with the validity of the ids used.

Tom Clarkson
@Tom Clarkson: I actively use the "web" in the custom method. I have made a change in its name and URL using AllowUnsafeUpdates hence I think "web"'s reference was changed when it returned. I'm thinking that it is the sole cause of problem, but have little ideas on how to solve it apart from nulling it somewhere.
BeraCim
Changing properties on a web object will not change the reference to that object. Setting it to null will not help, and the problem is clearly in the portion of the code you did not include in your question.
Tom Clarkson
I agree, the problem's in that method
kerray
Thanks guys. It turned out that another instance of web was returned from the custom method, and that was throwing the exception because the wrong object was getting garbage collected.
BeraCim