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.
In this post, let’s see how a small negligence, committing a transaction before it really finishes its work, could crash AutoCAD hard.
[CommandMethod("CommitTransactionTest")]
public static void CommitTransactionTest_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
try
{
Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
ObjectId id1 = ed.GetEntity("\nPick the 1st entity.").ObjectId;
ObjectId id2 = ed.GetEntity("\nPick the 2nd entity.").ObjectId;
if (id1.IsValid && id2.IsValid)
{
using (Transaction tr = dbTM.StartTransaction())
{
Entity ent1 = (Entity)id1.GetObject(OpenMode.ForWrite);
ent1.ColorIndex = 1; //Red
tr.Commit();
Entity ent2 = (Entity)tr.GetObject(id2, OpenMode.ForWrite);
ent2.ColorIndex = 2; //Yellow
tr.Commit();
}
}
else
{
ed.WriteMessage("\nNo all entities are properly selected.");
}
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
We cannot expect the second entity to be turned into yellow at all. Instead, after the command is run, two entities are selected, and a moment is waited, we will see AutoCAD crashes with the message ‘INTERNAL ERROR: Unhandled Access Violation Reading 0xffffffff Exception at 1a06063h’ titled with AutoCAD Error Aborting.
Give it a try and you will not miss it! More ways, some of which are pretty creative, will be demonstrated in the future. Please stay tuned.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Posted by: |