memory - System.AccessViolationException error only when reading large las files with C# Themis.Las library - Stack Overflow

admin2025-04-15  1

When i run this code with a small las file it works perfectly (40k points).But when the las file is bigger (21 million points) i get this error:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.Runtime.InteropServices.Marshal.IsPinnable(System.Object) at System.Runtime.InteropServices.GCHandle..ctor(System.Object, System.Runtime.InteropServices.GCHandleType) at System.Runtime.InteropServices.GCHandle.Alloc(System.Object, System.Runtime.InteropServices.GCHandleType) at Themis.Las.Stream.AsyncStreamHandler.MarshalPoints(Themis.Las.Stream.IStreamBuffer) at Themis.Las.Stream.AsyncStreamHandler.ReadPointsAsync()

My code:

using System;
using Themis.Las;

class Program
{
    static void Main(string[] args)
    {
        string lasFilePath = @"C:\Users\mjau\Desktop\LazFileReader\lidarData.las";
        try
        {
            using var reader = new LasReader(lasFilePath);
            var lpt = new LasPoint(); 
            int counter = 0;

            Console.WriteLine($"Reading LAS file: {lasFilePath}");
            while (!reader.EOF)
            {
                reader.GetNextPoint(ref lpt); 
                Console.WriteLine($"Point {counter + 1}: X={lpt.X}, Y={lpt.Y}, Z={lpt.Z}");
                counter++;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Critical Error: {ex.Message}");
        }
    }
}

I have tested both las files in an external program, they work fine.

转载请注明原文地址:http://www.anycun.com/QandA/1744720572a86690.html