title: Solidworks, tracking down a Memory Access Violation Error on Isldworks.CloseDoc
body: <p>I have two different functions inside an addon I have been working on in C#. Recently (Apparently) Solidworks has been crashing when it gets to certain parts of these two functions (possibly more, but these are the only two I have found it occurring in so far.) Under debug, both functions give me a "Memory Access Violation Error". This error occurs, every time, on the line where I am closing the active document, and occurs approximately 95% of the time.</p>
  
  <p>It is almost always on the same part.
  It seems to be independent of run time, or number of parts that have been opened and closed.
  If I dont close the files, I dont seem to get the error. But when running a large assembly, that presents its own issues.
  Adding in a 1s wait before closing seems to reduce the frequency of the error (As in, I can occasionally get through the entire assembly without the error)</p>
  
  <p>A quick explanation of what the function I am primarily concerned about is doing; It works from the top level of an assembly down, propagating the custom properties from the main assembly, and sub-assemblies, into their children. So I am constantly opening and closing different assembly and part files.</p>
  
  <p>The code below has been stripped down to pretty much the bare minimum that replicates the error. The error occurs on line 59. From what Ive seen online so far, it it seems like these are hard to track down. Any help is greatly appreciated.</p>
  
  <pre><code>    public void propagateProps(bool overwrite)  
      {  
          List&lt;string&gt; assemblies = new List&lt;string&gt;();  
          string topAssem;  
          string compName = "";  
          int i = 0;  
          int j = 0;  
          int errors = 0, warnings = 0;  
          int partType = 1;  
          swModel = iSwApp.ActiveDoc;  
          if (swModel == null)  
          {  
              MessageBox.Show("No assembly document open. Please open an assembly and try again.", "Avengers Assemble Error");  
              return;  
          }  
          if (swModel.GetType() != 2)  
          {  
              MessageBox.Show("No assembly document open. Please open an assembly and try again.", "Avengers Assemble Error.");  
              return;  
          }  
          topAssem = swModel.GetPathName();  
          assemblies.Add(swModel.GetPathName());  
          swAssy = iSwApp.ActiveDoc;  
          while (i &lt; assemblies.Count)  
          {  
              List&lt;string&gt; beenDone = new List&lt;string&gt;();  
              iSwApp.OpenDoc(assemblies[i], 2);  
              swModel = iSwApp.ActivateDoc(assemblies[i]);                  
              swAssy = iSwApp.ActiveDoc;  
              foreach (Component2 swComp in swAssy.GetComponents(true))  
              {  
                  partType = 1;  
                  compName = swComp.GetPathName();  
                  if (compName.IndexOf(").SLD") &gt; 0 || compName.IndexOf("REF") &gt; 0)  
                  {  
                      continue;  
                  }  
                  if (Path.GetExtension(compName).ToUpper() == ".SLDASM")  
                  {  
                      partType = 2;  
                      assemblies.Add(compName);  
                  }  
                  iSwApp.OpenDoc(compName, partType);  
                  swModel = iSwApp.ActivateDoc(compName);  
                  if (swModel == null)  
                  {  
                      continue;  
                  }  
  
  
                  #region things that might not be in  
  
  
              #endregion  
  
  
                  boolstatus = swModel.Save3(5, errors, warnings);  
                  System.Threading.Thread.Sleep(500);  
                  iSwApp.CloseDoc(swModel.GetPathName());  
              swPart = null;  
              swModel = null;  
              }  
              ++i;  
              System.Threading.Thread.Sleep(500);  
          }  
  
  
          return;  
      }  
  </code></pre>
  
  <p>Update: After seeing this question; <a href="https://stackoverflow.com/questions/30969974/whats-causing-the-memory-access-violation?rq=1">What&#39;s causing the memory access violation?</a> I tried messing with some global variables I use in my functions to no effect. I have however managed to wrap my essential code in a different logical structure for looping through parts which seems to be avoiding this issue. But I feel thats a band-aid at best and would like to be able to avoid this problem in the future.</p>