Вопрос

Does the below code leaks memory? If so, any recommendations on optimising it?

SPWeb web = (SPWeb)properties.Feature.Parent; // comes from the event receiver
//... lots of other code

// the below is the focal point.
foreach (SPWeb childWeb in web.Webs) 
{
    try
    {
        // lots of heavy processing with the childWebs
    }
    finally
    {
        if (childWeb != null)
        {
            childWeb.Dispose();
        }
    }
}
Это было полезно?

Решение

The code you have posted should be fine. However, depending on what you do with the childWeb within the try-statement, it might cause memory leaks. Can you post the entire code? Do you suspect memory leaks?

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

According to Disposing Objects, your code matches the Good Coding Practice for SPWeb.Webs.

As mentioned on that page, I would recommend downloading and using SPDisposeCheck as both verification of correct code and identification of potential memory leaks.

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