locked
Attempted to read or write protected memory. This is often an indication that other memory is corrupt. RRS feed

  • Question

  • Hi, all

    My problem is as follows: I'm programming an OPC for alarms and events in Visual Basic.net 2008 and I'm using a function in order to disconnect my opc client from the server.

    The function is:
    Declare Function DisconnectOPC_AE Lib "WTClient" Alias "_DisconnectOPC_AE@4" (ByVal hConnect As Integer) As Integer

    In my code I have the following:
    Dim myAEconnectionhandle As Long
    .
    .
    .
        Private Sub Disconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Disconnect.Click
            If myAEconnectionhandle <> -1 Then 'is different than -1 when it's connected
                Call DisconnectOPC_AE(myAEconnectionhandle)
                myAEconnectionhandle = -1 'set this value to disconnect
            End If
            SvrList.Enabled = True 'activates the button for searching the servers
            Disconnect.Enabled = False 'deactivates the button for disconnecting
        End Sub


    When my opc client is running and I try to disconnect, the following error appears

    AccesViolationException was unhandled:
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Can anybody help me???

    Thanks in advance

    Rodolfo


    Monday, October 20, 2008 4:05 PM

Answers

  • rojasglz said:

    When my opc client is running and I try to disconnect, the following error appears

    AccesViolationException was unhandled:
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Hi Rodolfo,

    Welcome to MSDN forums!

    I would like to provide you the following suggestions:

    Most of the time you will not see this kind of an error with purely .NET code. The CLR was designed to avoid these situations with developer code. However, when you go beyond purely managed code, an invalid pointer is what causes this problem. This can happen when you pass a pointer to an API. If the pointer is null (the address is 0), you will almost always get this error (assuming the unmanaged code doesn't crash first). If the pointer contains an address value that is outside the allocated memory range for the app, or is in the range reserved for OS usage, then you will also get the error. Additionally, you can map memory with different access levels (actually, as a .NET programmer, you usually don't, but some other unmanaged code might). If your process doesn't have "read" access to that mapped memory range, you'll also get this error.

    In short, most of the time it's a bug with your code trying to use unmanaged code (passing something by value instead of by ref, or passing a null pointer, or something to that effect), or it's a bug in a 3rd party unmanaged library (even possibly a driver depending on what kind of code we're interacting with here).

    Trackback: For further information, please take a look at the following thread:
    http://forums.msdn.microsoft.com/en-US/clr/thread/a6e81c9b-ff94-4bcb-ba28-33e226291044/


    In Visual Studio 2005/2008, you can consider setting the Debugger as the following steps:
    Tools menu -> Options -> Debugging -> General -> Uncheck option "Suppress JIT optimization on module load (Managed Only).

    There are many similar issues for you to check:
    http://forums.microsoft.com/msdn/Search/Search.aspx?words=Attempted+to+read+or+write+protected+memory&localechoice=9&SiteID=1&searchscope=allforums


    http://support.microsoft.com/search/default.aspx?catalog=LCID%3D1033&query=Attempted+to+read+or+write+protected+memory&mode=r



    Best regards,
    Martin Xie

    Friday, October 24, 2008 9:44 AM