by Fenton Webb
Recently I posted this blog entry on Performance, as you can see at the bottom it drove a lot of comments.
One question that came up was when to call Dispose(). The thing is that all of our .NET code wraps ObjectARX, in turn, all of our ObjectARX code is single threaded and the .NET Garbage Collector runs on a background thread... What this means is, if you don't call Dispose() on our AutoCAD .NET objects (the ones you create in your code) you run the high risk of the GC garbage collecting our objects on a background thread, which in turn may cause AutoCAD to crash.
That said, a crash happening really does depend on what the AutoCAD implementation of the Dispose code does under the hood. If the Dispose() simply does a memory free for which any destructors do nothing, then you’re probably going to be fine. However, if the dispose does other things like fire events, update UI, etc etc, then you are most likely looking at a crash. Therefore, I recommend you Dispose() all AutoCAD .NET objects (the ones you create in your code) using the .NET 'using' statement – that way you are not relying on the GC to clean up after yourself.
Now, say you are at a customer site where they are experiencing random crashes with your app, or you are running behind schedule and just can’t seem to find the place where the random crash is happening then here’s a quick fix for you – try forcing the GC to run on the Main Thread…
There is a setting that can be applied to the acad.exe.config file called gcConCurrent. See http://msdn.microsoft.com/en-us/library/yhwwzef8.aspx
An example of this being implemented in the acad.exe.config file is shown thus:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
<!--All assemblies in AutoCAD are fully trusted so there's no point generating publisher evidence-->
<runtime>
<generatePublisherEvidence enabled="false"/>
<gcConcurrent enabled="false" />
</runtime>
</configuration>