DEV Community

cadguide.tools
cadguide.tools

Posted on

AutoLISP & ObjectARX Memory Leak Mitigation in Batch DWG Processing: Managing Selection Sets and Transformed Matrix Caches

AutoLISP & ObjectARX Memory Leak Mitigation in Batch DWG Processing: Managing Selection Sets and Transformed Matrix Caches

CAD managers and developers frequently deploy batch script routines to process thousands of architectural DWG files—stripping orphaned proxy graphics, converting font styles, or exporting structural coordinate tables. However, long-running batch sessions inside AutoCAD, BricsCAD, or ZWCAD frequently grind to a crawl or trigger unhandled memory access violations (FATAL ERROR: Exception 0xC0000005) after processing 150 to 200 drawings.

The root cause is rarely the drawing complexity itself; it is uncollected C++ ObjectARX heap allocations and leaked AutoLISP selection set handles that persist across sequential drawing file switches.


1. Selection Set Table Exhaustion in AutoLISP

In the AutoLISP runtime engine, calling (ssget) allocates an entry in an internal C-level selection set pointer table. While modern machines have gigabytes of RAM, the AutoLISP interpreter historically maintains a hard-coded limit of 128 simultaneous active selection set handles:

;; BAD PRACTICE: Leaks selection set in memory loop
(defun c:BatchExportBad ( / dwgList)
  (foreach dwg dwgList
    (setq ss (ssget "X" '((0 . "INSERT"))))
    ;; ss is never freed; table overflows after 128 iterations
  )
)

;; ROBUST IMPLEMENTATION: Explicitly release and force garbage collection
(defun c:BatchExportClean ( / dwgList ss)
  (foreach dwg dwgList
    (setq ss (ssget "X" '((0 . "INSERT"))))
    (if ss
      (progn
        (ProcessBlockEntities ss)
        (setq ss nil)       ;; Dereference LISP pointer
        (gc)                ;; Invoke garbage collection
      )
    )
  )
)
Enter fullscreen mode Exit fullscreen mode

2. ObjectARX & C# .NET Transaction Management

When executing batch operations via the AutoCAD .NET API (accoremconsole.exe), failing to properly scope Transaction and Database objects causes native DWG entity memory to leak:

// ALWAYS dispose Database and Transaction objects explicitly
using (Database db = new Database(false, true))
{
    db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, false, "");
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
        // Perform batch geometry inspection
        tr.Commit();
    }
    // Database will be disposed cleanly, freeing unmanaged C++ heap
}
Enter fullscreen mode Exit fullscreen mode

If you suspect drawing corruption or version mismatch is causing batch script aborts, inspect raw binary header signatures directly in your browser with our client-side CAD DWG Version Checker.

For detailed registry tuning and DWG purge routines, review our technical guide on AutoCAD DWG Performance Optimization & Fatal Error Remediation.

Top comments (0)