Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Getting all points from Solid3D by using Brep randomly crashes AutoCAD with large objects

martin.folber
Advocate

Getting all points from Solid3D by using Brep randomly crashes AutoCAD with large objects

martin.folber
Advocate
Advocate

Hello,

please is anybody able to help us? We are struggling with critical AutoCAD crashing problem.

 

Visual Studio debugger shows an error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" in RXObject.DeleteUnmanagedObject()

 

    public static List<Point2d> GetPointsFromSolid3d(Solid3d ent, Transaction tr)
    {
      List<Point2d> points = new List<Point2d>();

 

      using (Autodesk.AutoCAD.BoundaryRepresentation.Brep brp = new Autodesk.AutoCAD.BoundaryRepresentation.Brep(ent))
      {
        foreach (var v in brp.Vertices)
        {
          Point2d p2d = v.Point.GetPoint2D();
          if (!points.Contains(p2d))
            points.Add(p2d);
        }

 

        GC.Collect();
        GC.WaitForPendingFinalizers();
      }

 

      return points;
    }

 

Is there another workaround, how to avoid random AutoCAD crashes? The purpose of this code is to get points for Civil 3D alignment station, 2D/3D area of solids and other important take-off parameters...

 

Thanks

Martin

 

0 Likes
Reply
289 Views
4 Replies
Replies (4)

ActivistInvestor
Advisor
Advisor

DeleteUnmanagedObject() is where the underlying native C++ destructor is called, and if it's called on the thread which the GC runs on (because the managed wrapper was not disposed), the outcome is often exactly what you are seeing.

 

This is most-likely a case of something that should be disposed, not being disposed, and it may not be in the code that you show because these types of errors do not happen until the GC tries to reclaim a managed wrapper and invokes its finalizer. So, the exceptions appear to happen randomly, but that's because objects are collected in a somewhat random manner.

 

So, you should review your code to see where you are using an object and not disposing of it when necessary. 

 

Also, I don't understand what the calls to GC are for in your code. They shouldn't be needed and the GC doesn't actually 'wait' for pending finalizers, unless you have configured AutoCAD to not run GC's concurrently.

0 Likes

michal_hcz
Contributor
Contributor

Hi,

Autocad randomly crashes with GC and without GC. We added it based on this topic:
https://forums.autodesk.com/t5/net/brep-api-autocad-crashes-when-deleting-unmanaged-object/m-p/11878...


Just basic collecting of points from Brep always crashes:

Mostly without any error report for Autodesk. Tested on Civil 24 and 25.

 

using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
	foreach (var entId in objectIds)
	{
		Entity ent = entId.GetObject(OpenMode.ForRead) as Entity;

		try
		{
			int count = 0;
			while (count < 20)
			{
				List<Point2d> points = new List<Point2d>();

				using (Autodesk.AutoCAD.BoundaryRepresentation.Brep brp = new Autodesk.AutoCAD.BoundaryRepresentation.Brep(ent))
				{
					foreach (var v in brp.Vertices)
					{
						Point2d p2d = v.Point.GetPoint2D();
						if (!points.Contains(p2d))
							points.Add(p2d);
					}
				}

				Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Entity id: " + ent.ObjectId.Handle.Value + " ======================================================");
				Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Brep points: " + points.Count);

				count++;
			}
		}
		catch (Exception ex)
		{
			Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
		}
	}
}

 




michal_hcz_0-1726644872176.pngmichal_hcz_1-1726644890673.png


-----Current Stack:
at AcBrBrepVertexTraverser.getVertex(AcBrBrepVertexTraverser*, AcBrVertex*)
at AcBrBrepVertexTraverser.getVertex(AcBrBrepVertexTraverser*, AcBrVertex*)
at Autodesk.AutoCAD.BoundaryRepresentation.BrepVertexEnumerator.get_Current()




0 Likes

kerry_w_brown
Advisor
Advisor

Can't make time to play ; 

 

Does dealing with vertices as Gilles did here help ??

https://forums.autodesk.com/t5/net/extracting-vertices-of-a-region-as-2d-points-list-in-c/m-p/941828...

 

Just a personal note:

adding a using directive woupd make the code more readable

using Autodesk.AutoCAD.BoundaryRepresentation;

 

Which AutoCAD build ?

 

If this is a defect in the AutoCAD Library I think we would have heard about it prior to this.
. . . but it may not hurt to submit a case.

 

Regards,

 

added: I wonder what the brp variable looks like in a debug locals pane ?

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

0 Likes

michal_hcz
Contributor
Contributor

Hi,

I've tried LINQ for vertices and the same result.

 

 

brp.Vertices.Select(_=>_.Point)

 

 

 

Civil is up to date:

michal_hcz_0-1726663991679.png

michal_hcz_1-1726664017335.png

 

 

brp variable in debugger:

It's difficult to get the variable when it crashes.

 

This one didn't crash.

michal_hcz_2-1726664164951.png

michal_hcz_3-1726664209770.png

 

 

0 Likes