Skip to content

A different and seemingly random exception occurs System.AccessViolationException/System.ExecutionEngineException/System.NullReferenceException/System.ExecutionEngineException #44519

@andreafi

Description

@andreafi

Running several times the same simple code below, a different and seemingly random exception occurs in different point of the code.

It only occurs on Net 5, instead it works on net core 3.1.
The problem appears to happen only on the x86 target platform.
My OS is windows 10.

Example of errors:

The following error occurs on row --> if (DebugWindowEnabled) Debug.Write(textToWrite);

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

System AccessViolationException

The following error occurs on row --> if (DebugWindowEnabled) Debug.Write(textToWrite);

System.ExecutionEngineException

System ExecutionEngineException

The followingerror occurs on row --> File.AppendAllText(LogFileName, textToWrite.ToString());

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System NullReferenceException

The followingerror occurs on row --> Task.Run(() => { DequeueLog(); });

System.ExecutionEngineException: 'Exception_WasThrown'

System ExecutionEngineException2

This is the Program class:

class Program
{
    static clsFileLog fileLogCyclesNumber = new clsFileLog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Log2");
    static void Main(string[] args)
    {
        for (int i = 0; i < 1000000; i++)
            fileLogCyclesNumber.WriteLine($"Test write LOG2");
    }
}

And this is the Log class:

public class clsFileLog : IDisposable
{
    public bool Enabled { get; set; } = true;
    public bool DebugWindowEnabled { get; set; } = true;
    public string LogFileName { get; set; }

    private ConcurrentQueue<string> logItems = new ConcurrentQueue<string>();
    private readonly object dequeueLocker = new object();

    public clsFileLog(string locLogFile)
    {
        LogFileName = locLogFile;
    }

    public void WriteLine(string strText)
    {
        Write(strText + "\n");
    }

    public void Write(string strText)
    {
        if (Enabled)
        {
            logItems.Enqueue(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "|" + strText);
            Task.Run(() => { DequeueLog(); });
        }
    }
    public void DequeueLog()
    {

        if (Monitor.TryEnter(dequeueLocker))
        {
            Thread.CurrentThread.Name = $"Log: {Path.GetFileName(LogFileName)}";

            try
            {
                bool WriteOk = false;
                Random waitRandNumber = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                while (!logItems.IsEmpty)
                {
                    string item = null;
                    StringBuilder textToWrite = new StringBuilder();
                    while (logItems.TryDequeue(out item) && textToWrite.Length < 1000000)
                        textToWrite.Append(item);
                    WriteOk = false;
                    do
                    {
                        try
                        {
                            if (DebugWindowEnabled) Debug.Write(textToWrite);

                            File.AppendAllText(LogFileName, textToWrite.ToString());
                            WriteOk = true;
                        }
                        catch
                        {
                            Thread.Sleep(waitRandNumber.Next(10, 100));
                        }
                    } while (WriteOk == false);

                    Thread.Sleep(100);
                }
            }
            catch { }
            Monitor.Exit(dequeueLocker);
        }
    }
    public void Dispose()
    {
        DequeueLog();
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions