I have this code that runs fine in acad versions prior to 2025, now trying to compile against acad 2025 and dot net 8, the cad session freezes than crashes even though my routine is wrapped in a "try...catch" block which acad promptly ignores
internal void CleanUpTransientObjects()
{
TM.EraseTransients(TransientDrawingMode.DirectShortTerm, 0, []); // this executes ok, but does nothing
foreach (Drawable d in Letters) d.Dispose(); // this causes crash
Letters.Clear(); // this causes crash too, any of the last two lines will freeze/crash the cad session
}
Exception thrown is:
System.AccessViolationException Attempted to read or write protected memory. This is often an indication that other memory is corrupt
Again - only v2025 seems to be affected by this for some reason, does not make a difference whether i run the .net 4.6 framework version or the .net 8 version
Any hints?
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
@TLatev ,
Here are what I tested (see code below).
Notice that the line of code that erases the Transient graphics could be different
1. If erasing is done by calling EraseTransient() (that is, erase each Transient);
2. Erase all Transient by calling EraseTransients(), with the second argument being 128, which is what I ALWAYS use.
3. Erase all Transient by calling EraseTransients(), with the second argument being 0 (as you did);
The result:
1. Build the code against .NET 4.8 (so, it runs with all versions of AutoCAD, including Acad2025). No problem of testing all 3 lines of code tested with Acad2024. When testing with Acad2025 (.NET 4.8 based code), the 3 of the above code crashes AutoCAD2025;
2. Build the code with .NET 8, thus the code will only run Acad2025. Again, code 1 and 2 work OK, and code 3 crashes Acad2025.
So, the issue is the second argument used for EraseTransients() cannot be 0. (note, it is OK for Acad2024). Due to the notoriously lacking of documentation, I am not sure what values of the second argument means, but I always used 128🤔. Since it works with Acad2024 or older when the value being 0, I'd consider it is a bug introduced by Acad2025, be the code is based on .NET 4.X or .NET 8. So, while you may want to hold your breath waiting Autodesk to fix it, you can try to simply pass 128 to the method instead to make the code work in all versions of AutoCAD.
Here is my text code, which can be built against both .NET4.X or .NET 8:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(Transients2024.MyCommands))]
namespace Transients2024
{
public class MyCommands
{
private Point3d _center;
private Circle _circle = null;
private TransientManager _ts = TransientManager.CurrentTransientManager;
[CommandMethod("MyCmd")]
public void RunMyCommand()
{
var dwg = CadApp.DocumentManager.MdiActiveDocument;
var ed = dwg.Editor;
var res = ed.GetPoint("\nSelect center point:");
if (res.Status != PromptStatus.OK) return;
_center = res.Value;
try
{
_circle = new Circle() { Center = _center, Radius = 1, ColorIndex = 2 };
_ts.AddTransient(
_circle, TransientDrawingMode.DirectShortTerm, 128, new IntegerCollection());
ed.PointMonitor += Ed_PointMonitor;
res = ed.GetPoint("\nPick radius:");
}
finally
{
ed.PointMonitor -= Ed_PointMonitor;
ed.WriteMessage("\nIn finally clause!");
// ===== this line always work ========================
//_ts.EraseTransient(_circle, new IntegerCollection());
// ====================================================
// ==== This line always work: the second argument is 120 ==============================
_ts.EraseTransients(TransientDrawingMode.DirectShortTerm, 128, new IntegerCollection());
// =====================================================================================
// ==== This is the problematic line, with the second argument being 0==================
// it works with Acad2024 or older, but crash Acad2025, be the code is built against
// .NET 4.x, or against .NET 8 (of course, for Acad2024, no .NET 8 build)
//_ts.EraseTransients(TransientDrawingMode.DirectShortTerm, 0, new IntegerCollection());
// =====================================================================================
_circle.Dispose();
}
}
private void Ed_PointMonitor(object sender, PointMonitorEventArgs e)
{
var pt = e.Context.RawPoint;
var r = pt.DistanceTo(_center);
if (r > 0)
{
_circle.Radius = r;
_ts.UpdateTransient(_circle, new IntegerCollection());
}
}
}
}
Norman Yuan
Thanks Norman,
This is really helpful - indeed I suspected that some kind of regression was introduced in v2025, but wouldn't have thought to experiment with random powers of two for the XXXTransient functions parameters.
The code runs fine now, I'll stop by to buy you a beer next time I find myself in Edmonton 😉
Can't find what you're looking for? Ask the community or share your knowledge.