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.
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)
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.
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.
public void propagateProps(bool overwrite)
{
List<string> assemblies = new List<string>();
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 < assemblies.Count)
{
List<string> beenDone = new List<string>();
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") > 0 || compName.IndexOf("REF") > 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;
}
Update: After seeing this question; What's causing the memory access violation? 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.
PiCalculator.BeginCalc( digits = 30000 )
), or some simple statechange -myForm.BeginInvoke( (Action) delegate() { button.Text = "Sending.."; } )
– Podium