In the past C++/C era, it was very easy to crash AutoCAD or even the whole Operating System. Now in the .NET time, it is harder, much harder than before, but we still can find various ways to crash AutoCAD using its .NET API.
As well known, we’d better dispose of those newly created AutoCAD DB objects or entities that are not supposed to be or have not been successfully added to the AutoCAD database. We have to dispose of those objects opened by the ObjectId.Open calls as demonstrated a few times before; otherwise, crash will just happen. Using using for .NET objects not only saves the explicit Dispose() calls but also guarantees that the objects will get disposed of when out of the using scope in either normal situations or exceptions.
However, please do not abuse using AutoCAD .NET objects. Some are not good for using, e.g. the MDI Active Document (MdiActiveDocument); otherwise, AutoCAD will behave abnormally and finally crash too.
[CommandMethod("CrashAutoCAD12")]
public static void CrashAutoCAD12_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument)
{
// Do anything to the doc.
}
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex.Message);
}
}
After the command is run, an AutoCAD message saying “Unhandled exception has occurred in a component in your application. If you click Contine, the application will ignore this error and attempt to contine. Object reference not set to an instance of an object.” as follows will show up.
If the Details button is clicked, we will see some code tracking that seems to have nothing to do with our simple test code. Instead, it mentions some idling and quiescent entering events! If the Continue button is pressed, the same error message box will pop up repeatedly, so we cannot really continue anymore. Behaving like this is even worse than a direct kaboom!
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Recent Comments