Вопрос

I have an issue in my SharePoint environment and I suspect that this code causes a memory leak. I'm passing a SPWeb object as a value parameter.

Will the SPWeb object instance get disposed correctly using this code?

public void DoSomething(SPWeb web)
{
    // code here
}
public void mainProgram()
{
    using (SPWeb web = site.OpenWeb())
    {
        DoSomething(web);
        //another code here
    }
}
Это было полезно?

Решение

When you pass an object to a method by value, in the method you get simply copy of reference to the same object. No new objects is created, this mean that your code is correct. We need to dispose such object as spweb and spsite because they are using external resources (that require memory), and this resources not destroyed by garbage collector, we had to explicitly call method to remove it and free memory.
When you stop using your web object (right after "}") external resource for this particular object will be free, that's why all is correct.
You can try to verify your code using SPDisposeCheck tool. From my practice when you are using SPLimitedWebPartManager you need to explicitly dispose internal web webPartManaber.Web.Dispose(); (that is not so obvious at a first glance)

Другие советы

That code will dispose the SPWeb object correctly with the using statement. If an exception is thrown in the method, it will still get disposed.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top