0

I'm developing a Windows program that interacts with a physical machine via TCP, and pulls data from it.

The data more specifically is represented by a class, let's say MachineData:

    readonly public struct MachineData
    {
        readonly double X;
        readonly double Y;
        readonly double Z;
        readonly long feedrate;
        readonly ulong timestamp;
        readonly ulong id;
        readonly int laserPower;
        readonly int spotArea;
        readonly int spotWidth;

        public MachineData(double x, double y, double z, long _feedrate, ulong _timestamp, ulong _id, int _laserPower, int _spotArea, int _spotWidth)
        {
            X = x;
            Y = y;
            Z = z;
            feedrate = _feedrate;
            timestamp = _timestamp;
            id = _id;
            spotArea = _spotArea;
            spotWidth = _spotWidth;
            laserPower = _laserPower;
        }

        public string ToCSV()
        {
            try
            {
                return String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0},{1},{2:0.000},{3:0.000},{4:0.000},{5},{6},{7},{8}\n", id, timestamp, X, Y, Z, feedrate, laserPower, spotArea, spotWidth);
            }
            catch
            {
                return "ERROR";
            }
        }

        public override string ToString()
        {
            return String.Format("X: {0:0.000} Y: {1:0.000} Z: {2:0.000} Feedrate: {3} Timestamp: {4} ID: {5} Laser: {6}, Spot Area: {7}, Spot Width: {8}", X, Y, Z, feedrate, timestamp, id, laserPower, spotArea, spotWidth);
        }
    }

So I have one thread pulling data, and enqueuing it in a ConcurrentQueue<MachineData> data structure.

This MachineData class has a ToCSV() method that formats it in the export format. This method is basically:

public string ToCSV()
        {
            try
            {
                return String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0},{1},{2:0.000},{3:0.000},{4:0.000},{5},{6},{7},{8}\n", id, timestamp, X, Y, Z, feedrate, laserPower, spotArea, spotWidth);
            }
            catch
            {
                return "ERROR";
            }
        }

Another thread is dequeuing the data and writing it to a file. When the program runs for about 3 minutes, it crashes with the error:

Fatal error. Internal CLR error. (0x80131506) when on .NET Core 3.1. The Windows debugger points the error to that line:

return String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0},{1},{2:0.000},{3:0.000},{4:0.000},{5},{6},{7},{8}\n", id, timestamp, X, Y, Z, feedrate, laserPower, spotArea, spotWidth);

I have no idea. Anyone could help me?

Hiram
  • 143
  • 1
  • 8
  • I wouldn't read too much into the line it crashed on: something else probably went and corrupted something, but it only actually fell over on that line. I'm afraid you'll have to cast your net a bit wider: suspect any interop, native or unsafe code first. – canton7 Jun 18 '21 at 16:35
  • Okay. I have a really huge library class that calls functions from the DLL that connects to the machine. More specifically, it's the FANUC FOCAS library, which is used for CNC interfacing. This library fwlib64.cs has an extensive list of Interop calls. I'm afraid this may be the problem. – Hiram Jun 18 '21 at 16:42
  • That seems likely, I'm afraid! – canton7 Jun 18 '21 at 16:42
  • Also, I have noticed that no garbage collection occurs, and the memory usage keeps increasing (natural for the large number of object instantiation). I think that when the GC tries to run, this problem happens. – Hiram Jun 18 '21 at 16:43
  • If the crash is reliably on that line, that seems unlikely. The GC normally happens pretty non-deterministically – canton7 Jun 18 '21 at 16:57
  • @canton7 Most of the time the error is on that line, but i've seen it it other regions as well... – Hiram Jun 20 '21 at 16:56

0 Answers0