Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

25 Oct

Once in a while some annoying exceptions occur…

The exception

Exception in SearchBoxEx::CreateChildControls:System.TypeInitializationException: The type initializer for 'Microsoft.SharePoint.Portal.WebControls.LocStringIdLookupTable' threw an exception. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
 at Microsoft.SharePoint.Portal.WebControls.LocStringIdLookupTable..cctor()

And the inner exception stack trace gave me more info:

at Microsoft.SharePoint.Portal.WebControls.StringResourceManager.ConvertLocStringIdToStringFast(LocStringId lsid)
 at Microsoft.SharePoint.Portal.WebControls.StringResourceManager.GetString(LocStringId lsid)
 at Microsoft.SharePoint.Portal.WebControls.SiteInfo.get_IsUICultureRTL()
 at Microsoft.SharePoint.Portal.WebControls.SearchBoxEx.CreateChildControls()

Investigation

So the exception occurred in the SearchBoxEx control, method CreateChildControls. Since this is a stack trace of standard SharePoint Server code I decompiled the standard SharePoint Server assemblies which I needed. This is necessary because this is no custom code belonging to an application we created. After analyzing the code it seems that when creating the SearchBoxEx control a context is created to build the control and its properties. During the creation of this context a SPSite and SPWeb are needed.
SharePoint is lacking to dispose these objects.
Another site is also using this SearchBoxEx control and no memory leaks have been reported here. The searchbox control used at this site is the same, but the context is different. The searchbox in this site uses its own SPWeb as context and this SPWeb object doesn’t need any disposal.

The SearchBoxEx control can be found at the top of the pages on the right hand side and in the Search center. The PeopleSearchBoxEx is a different control and doesn’t leak any memory.

How does the SearchBoxEx control get its context?

This is the code:


private SPWeb GetSpWeb()
{
  using (new SPMonitoredScope("SearchBoxEx.GetSpWeb"))
  {
    SPWeb personalWeb = null;
    if ((this.m_strCtxScopeFromGetOverride == SearchCommon.GetLocResourceString(LocStringId.SearchBox_ThisSite_Label)) && !string.IsNullOrEmpty(this.m_strSearchWithInUrlGetOverride))
    {
      if (this._PropertiesOverrideableBySite.EffectiveSearchResultPageUrl.EndsWith("/_layouts/OssSearchResults.aspx", StringComparison.OrdinalIgnoreCase))
      {
        personalWeb = SPContext.GetContext(HttpContext.Current).Web;
      }
      if (personalWeb == null)
      {
        SPSite site;
        try
        {
          site = new SPSite(this.m_strSearchWithInUrlGetOverride);
        }
        catch (FileNotFoundException exception)
        {
          site = null;
          ULS.SendTraceTag(0x66673974, ULSCat.msoulscat_SEARCH_Query, ULSTraceLevel.Medium, string.Concat((string[]) new string[] { "The SearchBox was passed an invalid site url in the query parameter: url={", this.m_strSearchWithInUrlGetOverride, "} Exception Detail: message={", exception.get_Message(), "} name={", exception.get_FileName(), "}" }));
        }
        if (site != null)
        {
          personalWeb = site.OpenWeb();
        }
      }
    }
    if (personalWeb == null)
    {
      if (this.Page is MySitePublicWebPartPage)
      {
        IPersonalPage page = this.Page as IPersonalPage;
        personalWeb = page.PersonalWeb;
      }
      if (personalWeb == null)
      {
        personalWeb = SPContext.GetContext(HttpContext.Current).Web;
      }
      if (personalWeb == null)
      {
        throw new ArgumentNullException("SPSite is null. No contextual scopes will be added");
      }
    }
    return personalWeb;
  }
}

The code explicitly checks if the result page ends with “/_layouts/OssSearchResults.aspx” to use the current context or to create an SPSite/SPWeb.

The personalWeb in the above code is returned by this method to the calling method but the SPSite site isn’t disposed!

This method returns the personalWeb to the calling method ‘HandleContextualScoping’. This SPWeb is used in the method, but not disposed!

Summary

Oeps.

Microsoft, would you be so kind to fix this?

2 Replies to “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

  1. Hey Anita,

    Great find, what version of SharePoint do you have ?

    Have you contacted Microsoft, if so, what have they told you ?

  2. Hi Christian,

    SharePoint Server 2010 is what I used and I didn’t contact Microsoft about this issue. I assume this is on their list…

    Regards, Anita

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.