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

Atillio Fetter 6 Reputation points
2022-08-09T13:10:52.933+00:00

System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
Error with the ntdll.dll

I am building a software to communicate with a led controller card, the software uses UDP communication with a async process that's always waiting for a new message, when this message arrives it’s handled with a delegate method and its shown on a rich text box, this is the only “out of ordinary” part of the code, I will print this below, but the error does not occur there, when I enable or disable a panel, but that’s very weird, I will paste a part of the code because I can’t post the full code here.

 private void StartListening()  
    {  
        if (!form_closed)  
            Client.BeginReceive(Receive, new object());  
    }  

    private void Receive(IAsyncResult ar)  
    {  
        try  
        {  
            ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);  
            byte[] bytes = Client.EndReceive(ar, ref ip);  
            rec_msg = Encoding.ASCII.GetString(bytes);  
            if (rec_msg != "broadcast_teste")  
            {  
                ShowMessage(richTextBox1, rec_msg);  
                 
            }  
        }  
        catch (Exception ex)  
        {  
            ShowMessage(richTextBox1, ex.Message);  
        }  

        StartListening();  
    }  

delegate void ShowMessageDelegate(RichTextBox richtextbox1, string message);

    private void ShowMessage(RichTextBox richtextbox, string message)  
    {  
        if (richtextbox.InvokeRequired)  
        {  
            ShowMessageDelegate showMessageDelegate = ShowMessage;  
            richtextbox.Invoke(showMessageDelegate, new object[] { richtextbox, message });  
        }  
        else  
        {                  

          // some simple logic to write data on text boxes //  

            richtextbox.Text += message + "\r\n";  
            richtextbox.SelectionStart = richtextbox.TextLength;  
            richtextbox.ScrollToCaret();  
        }  
    }
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} vote

2 answers

Sort by: Most helpful
  1. Michael Taylor 61,101 Reputation points
    2022-08-09T15:13:39.343+00:00

    It looks like you're accessing the UI outside the UI thread which can lead to exceptions on any thread at any point in time. But there is insufficient information to be sure. What does the call stack of the exception look like? Is there any inner exception details?

    I don't see what the second screenshot has to do with the first one. If this is where the actual error is occurring then you'll need to provide us the context in which this method is being called. It looks like it is being called on a non-UI thread to me.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 81,981 Reputation points Volunteer Moderator
    2022-08-12T21:04:57.277+00:00

    If you want to update the ui from a different thread you should use the Dispatcher class

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher?view=windowsdesktop-6.0

    Also because the callback is async, you should check if the window is valid before updating.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.