{"id":1482,"date":"2012-10-25T16:30:45","date_gmt":"2012-10-25T14:30:45","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1482"},"modified":"2012-10-25T16:35:14","modified_gmt":"2012-10-25T14:35:14","slug":"attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-other-memory-is-corrupt","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-other-memory-is-corrupt\/","title":{"rendered":"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."},"content":{"rendered":"<p>Once in a while some annoying exceptions occur\u2026<\/p>\n<h3>The exception<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nException in SearchBoxEx::CreateChildControls:System.TypeInitializationException: The type initializer for 'Microsoft.SharePoint.Portal.WebControls.LocStringIdLookupTable' threw an exception. ---&gt; System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.\r\n at Microsoft.SharePoint.Portal.WebControls.LocStringIdLookupTable..cctor()\r\n<\/pre>\n<p>And the inner exception stack trace gave me more info:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nat Microsoft.SharePoint.Portal.WebControls.StringResourceManager.ConvertLocStringIdToStringFast(LocStringId lsid)\r\n at Microsoft.SharePoint.Portal.WebControls.StringResourceManager.GetString(LocStringId lsid)\r\n at Microsoft.SharePoint.Portal.WebControls.SiteInfo.get_IsUICultureRTL()\r\n at Microsoft.SharePoint.Portal.WebControls.SearchBoxEx.CreateChildControls()\r\n<\/pre>\n<h3>Investigation<\/h3>\n<p>So the exception occurred in the <em>SearchBoxEx <\/em>control, method <em>CreateChildControls<\/em>. 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.<br \/>\nSharePoint is lacking to dispose these objects.<br \/>\nAnother 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\u2019t need any disposal.<\/p>\n<p>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\u2019t leak any memory.<\/p>\n<h4>How does the SearchBoxEx control get its context?<\/h4>\n<p>This is the code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nprivate SPWeb GetSpWeb()\r\n{\r\n  using (new SPMonitoredScope(&quot;SearchBoxEx.GetSpWeb&quot;))\r\n  {\r\n    SPWeb personalWeb = null;\r\n    if ((this.m_strCtxScopeFromGetOverride == SearchCommon.GetLocResourceString(LocStringId.SearchBox_ThisSite_Label)) &amp;&amp; !string.IsNullOrEmpty(this.m_strSearchWithInUrlGetOverride))\r\n    {\r\n      if (this._PropertiesOverrideableBySite.EffectiveSearchResultPageUrl.EndsWith(&quot;\/_layouts\/OssSearchResults.aspx&quot;, StringComparison.OrdinalIgnoreCase))\r\n      {\r\n        personalWeb = SPContext.GetContext(HttpContext.Current).Web;\r\n      }\r\n      if (personalWeb == null)\r\n      {\r\n        SPSite site;\r\n        try\r\n        {\r\n          site = new SPSite(this.m_strSearchWithInUrlGetOverride);\r\n        }\r\n        catch (FileNotFoundException exception)\r\n        {\r\n          site = null;\r\n          ULS.SendTraceTag(0x66673974, ULSCat.msoulscat_SEARCH_Query, ULSTraceLevel.Medium, string.Concat((string&#x5B;]) new string&#x5B;] { &quot;The SearchBox was passed an invalid site url in the query parameter: url={&quot;, this.m_strSearchWithInUrlGetOverride, &quot;} Exception Detail: message={&quot;, exception.get_Message(), &quot;} name={&quot;, exception.get_FileName(), &quot;}&quot; }));\r\n        }\r\n        if (site != null)\r\n        {\r\n          personalWeb = site.OpenWeb();\r\n        }\r\n      }\r\n    }\r\n    if (personalWeb == null)\r\n    {\r\n      if (this.Page is MySitePublicWebPartPage)\r\n      {\r\n        IPersonalPage page = this.Page as IPersonalPage;\r\n        personalWeb = page.PersonalWeb;\r\n      }\r\n      if (personalWeb == null)\r\n      {\r\n        personalWeb = SPContext.GetContext(HttpContext.Current).Web;\r\n      }\r\n      if (personalWeb == null)\r\n      {\r\n        throw new ArgumentNullException(&quot;SPSite is null. No contextual scopes will be added&quot;);\r\n      }\r\n    }\r\n    return personalWeb;\r\n  }\r\n}\r\n<\/pre>\n<p>The code explicitly checks if the result page ends with &#8220;\/_layouts\/OssSearchResults.aspx&#8221; to use the current context or to create an SPSite\/SPWeb.<\/p>\n<p>The personalWeb in the above code is returned by this method to the calling method but the SPSite <em>site<\/em> isn\u2019t disposed!<\/p>\n<p>This method returns the personalWeb to the calling method \u2018HandleContextualScoping\u2019. This SPWeb is used in the method, but not disposed!<\/p>\n<h3>Summary<\/h3>\n<p>Oeps.<\/p>\n<p>Microsoft, would you be so kind to fix this?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once in a while some annoying exceptions occur\u2026 The exception Exception in SearchBoxEx::CreateChildControls:System.TypeInitializationException: The type initializer for &#8216;Microsoft.SharePoint.Portal.WebControls.LocStringIdLookupTable&#8217; threw an exception. &#8212;&gt; System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-other-memory-is-corrupt\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[42],"class_list":["post-1482","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-sharepoint-2010"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1482"}],"version-history":[{"count":15,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1482\/revisions"}],"predecessor-version":[{"id":1497,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1482\/revisions\/1497"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}