title: SharePoint 2010 "unhandled win32 exception occurred in W3WP.EXE"
body: <p>I am getting an win32 exception that I need help debugging.  I have a SharePoint page that has a custom Web Part that when I browse to that page, I get the following message:</p>
  
  <pre><code>In w3wp.dmp the assembly instruction at ntdll!RtlReportCriticalFailure+62 in 
  C:\Windows\System32\ntdll.dll from Microsoft Corporation has caused an unknown
  exception (0xc0000374) on thread 43
  
  Unhandled exception at 0xHEX in w3wp.exe: 0xc0000374: 
  A heap has been corrupted
  </code></pre>
  
  <p>I don't really know where to begin debugging this issue and searching the internet has yielded very little results.</p>
  
  <p>I have saved the DMP file and I've loaded it into the Debug Diagnostic Tool for analysis. I don't know what parts are most important, so if someone could tell me which sections I should post, I will post them.</p>
  
  <h2>Where I think the problem is:</h2>
  
  <p>I have narrowed down the location of where the problem <em>might</em> be.  If I don't call these methods, I don't get the error.  Is there something wrong with the way I'm accessing the TermStore?</p>
  
  <pre><code>    private void GetTerms()
      {
          using (SPSite site = new SPSite(PhaseListUrl.Value))
          {
              var session = new TaxonomySession(site);
              if (session.TermStores == null || session.TermStores.Count == 0) return;
  
              OfficeDropdown.Items.Clear();
              OfficeDropdown.Items.Add("");
  
              var termStore = session
                  .TermStores
                  .FirstOrDefault(ts =&gt; ts.Groups.Any(g =&gt; g.Name == TaxonomyName.Value));
              try
              {
                  var group = termStore.Groups[TaxonomyName.Value];
                  foreach (var ts in group.TermSets)
                  {
                      if (!ts.IsAvailableForTagging) continue;
  
                      AddTerms(site, termStore, ts.Id, ts.Terms);
                  }
              }
              catch (NullReferenceException) { OfficeDropdown.Items.Add("ERROR: Check your Org Term Store Name"); }
              catch (ArgumentOutOfRangeException) { OfficeDropdown.Items.Add("ERROR: Check your Org Term Store Name"); }
          }
      }
  
      private void AddTerms(SPSite site, TermStore termStore, Guid tsId, TermCollection coll)
      {
          foreach (var t in coll)
          {
              if (t.IsAvailableForTagging)
              {
                  var wssIds = TaxonomyField.GetWssIdsOfTerm(site, termStore.Id, tsId, t.Id, true, 1000);
  
                  if (wssIds.Length != 0)
                  {
                      var id = wssIds[0];
                      var validatedString = string.Format("{0};#{1}{2}{3}", id, t.Name, TaxonomyField.TaxonomyGuidLabelDelimiter, t.Id);
                      OfficeDropdown.Items.Add(new ListItem(t.Name, validatedString));
                  }
                  else
                  {
                      int id = -1;
                      var value = new TaxonomyFieldValue(SPContext.Current.Web.AvailableFields[new Guid("{a64c1f69-c0d6-421a-8c8a-9ef8f459d7a2}")]);
                      value.TermGuid = t.Id.ToString();
  
                      var dummy = value.ValidatedString;
                      if (dummy != null)
                          id = value.WssId;
  
                      var validatedString = string.Format("{0};#{1}{2}{3}", id, t.Name, TaxonomyField.TaxonomyGuidLabelDelimiter, t.Id);
                      OfficeDropdown.Items.Add(new ListItem(t.Name, validatedString));
                  }
              }
              AddTerms(site, termStore, tsId, t.Terms);
          }
      }
  </code></pre>
  
  <h2>UPDATE:</h2>
  
  <p>The bug was in the <code>TaxonomyField</code>.  I was accessing terms cross site collection and the IDs hadn't been generated.  Also look out for <code>TermStore</code> IDs not being the same cross site collection (and know where you are calling from and where you are calling to!)</p>