I can't batch process 3dm files using Rhino.Inside

I created a console project with Rhino.Inside (via nuget) to generate a json for each 3dm file.
My code it’s very similar to this sample: rhino-developer-samples/rhino.inside/dotnet/SampleConvert/Program.cs at 7 · mcneel/rhino-developer-samples · GitHub
However it crushes each 2-4 files, with this error:

Unhandled exception at 0x00007FFE052B49EE (ucrtbase.dll) in Analyze3dms.exe: Fatal program exit requested.

 internal class Program
 {
     static Program()
     {
         RhinoInside.Resolver.Initialize();
     }

     [STAThread]
     static void Main(string[] args)
     {
         try
         {
             using (var rhCore = new RhinoCore(new string[] { "/NOSPLASH" }, WindowStyle.NoWindow))
             {
                 var destinationFolder = @"C:\...";
                 var folders3dm = new string[] {
                     @"C:\..." , @"C:\...", @"C:\..." 
                 };

                 var map3dm = new Dictionary<string, string>(); // maps file name to file path
                 foreach (var folder in folders3dm)
                 {
                     var files = Directory.GetFiles(folder, "*.3dm").ToList();
                     if (files.Count == 0)
                     {
                         foreach (var subFolder in Directory.GetDirectories(folder))
                         {
                             files.AddRange(Directory.GetFiles(subFolder, "*.3dm"));
                         }
                     }
                     foreach (var file in files)
                     {
                         var name = Path.GetFileNameWithoutExtension(file);

                         if (map3dm.ContainsKey(name))
                         {
                             //Console.WriteLine($"{name} duplicated: {map3dm[name]}, {file}");
                         }
                         else
                         {
                             //Console.WriteLine(name);
                             map3dm.Add(name, file);
                         }
                     }
                 }

                 var counter = 0;
                 foreach (var kvp in map3dm)
                 {
                     counter++;
                     var newJsonFile = Path.Combine(destinationFolder, kvp.Key + ".json");
                     if (File.Exists(newJsonFile))
                     {
                         continue;
                     }

                     Console.WriteLine($"{counter}/{map3dm.Count} | {kvp.Key} | {kvp.Value}");

                     var doc = RhinoDoc.Open(kvp.Value, out _);
                     if (doc == null)
                     {
                         Console.WriteLine("doc is null");
                         Console.WriteLine();
                         continue;
                     }

                     var sku = SKU.CreateFrom3dm(doc); // This just reads layer info, it's not causing the problem bc I'm getting same error if I don't execute it.

                     //doc.Dispose(); // I tried with and without Dispose, no difference.
 
                     SKU.WriteJson(newJsonFile, sku);
                     Console.WriteLine();

                     System.Threading.Thread.Sleep(1000);

                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         } 
     } 
 }

I’m quite sure the problem is at openning the doc.

I had written this far when I tried to disable “Enable native code debugging” and this has eliminated that error.

However, the problem continues. I can randomly process 1 to 20 files without problems, but it either stops on its own, or it doesn’t progress, or I get an error: System.AccessViolationException: ‘Attempted to read or write protected memory. This is often an indication that other memory is corrupt.’

  • What else can I try?
  • Is there a way to load RhinoInside without plugins?
  • Can I load Rhino 8 instead of Rhino 7?

I can share the project and files with some McNeel stuff.

Thanks.