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

  • Question

  •  

    Hi All,

     

    I am using VB.net2005 and interop

    Code snipnet:

     

    Dim objpropsets As Test.PropAuto.Interop.PropertySets = Nothing

    Dim objprops As Test.PropAuto.Interop.Properties = Nothing

    Dim objprop As Test.PropAuto.Interop.Property = Nothing

    Try

    objpropsets = CreateObject("Test.FileProperties")

    objpropsets.Open(FileName)

     

    Exception is occuring at objpropsets.Open(FileName).  - Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

     

    Can you please help me in the same???

    Tuesday, October 23, 2007 5:36 PM

Answers

  • Rups11,

     

    Based on your post, the error message is quite a common issue. It seems that you are developing the application with the unmanaged code Interop. I would like to provide you the suggestions as follows:

     

    1. 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).

     

    For further information, please take a look at the following thread and the above explanation is provided by Rob Teixeira :

     

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

     

    2. In Visual Studio 2005, you can consider to set the Debugger as the following steps:

     

    In VS menu, click Tools -> Options. In the Options panel, please choose Debugging -> General. Then please uncheck option "Suppress JIT optimization on module load (Managed Only).

     

    Hope that can help you.

    Thursday, October 25, 2007 9:18 AM

All replies

  • Rups11,

     

    Based on your post, the error message is quite a common issue. It seems that you are developing the application with the unmanaged code Interop. I would like to provide you the suggestions as follows:

     

    1. 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).

     

    For further information, please take a look at the following thread and the above explanation is provided by Rob Teixeira :

     

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

     

    2. In Visual Studio 2005, you can consider to set the Debugger as the following steps:

     

    In VS menu, click Tools -> Options. In the Options panel, please choose Debugging -> General. Then please uncheck option "Suppress JIT optimization on module load (Managed Only).

     

    Hope that can help you.

    Thursday, October 25, 2007 9:18 AM
  •  

    Hi Rups11 & Bruno,

     

    I am having the same issue with Rups11 when using VS.Net 2005 with Interop dll of Crystal Reports.

    However, I cannot locate which pointer/ reference in the program to have such bad/null value.

    One more supplementary information, the code is runnable without any problem in VS.Net 2003.

    During upgrade, I don't make any big change to the code but just update the Project References to latest version.

    Does Rups11 solve the issue?

    Any more suggestion?

     

    AxInterop.CRVIEWER9Lib.dll -> AxInterop.CrystalActiveXReportViewerLib11_5.dll

    Interop.CRAXDRT.dll (v9)-> Interop.CRAXDRT.dll (v11)

    Here is fragment of code:

    Dim adoOracleDbConnection As OracleConnection        

    Dim adoOracleDbDataAdapter As OracleDataAdapter        

    Dim conn As String        

    Dim strSQL As String        

    Dim Report As New CRAXDRT.Report        

    Dim CrApp As New CRAXDRT.Application        

    Dim myXmldocument As New Xml.XmlDocument        

    Try

                 myXmldocument.Load(Application.StartupPath & "/CRpath.xml")

                 conn = "connection_string"

                 adoOracleDbConnection = New OracleConnection(conn)

                 strSQL = Report_SQL

                 adoOracleDbDataAdapter = New OracleDataAdapter(strSQL, adoOracleDbConnection)

                 adoOracleDbDataAdapter.Fill(dataSet, "Main")

                 Report = CrApp.OpenReport(tempReportSource)'<---exception prompt here ocasionally

                 Report.Database.Tables(1).SetDataSource(dataSet.Tables(0))

                 AxCRViewer11.ReportSource = Report

                 AxCRViewer11.ViewReport()

    Catch x As Exception

                 Console.WriteLine("Form1_Load:" & x.Message & Chr(13) & (10) & x.StackTrace) 

                Exit Sub        

    End Try

     

     

    Thanks and Regards,

    Cherry

     

    Wednesday, November 28, 2007 1:58 AM
  • Hello, all.

    I'm having the same issue.

    Here's the typedef of a function that works when called from C++:

    typedef char*(*DLLFUNC_Encrypt)(const char*, unsigned int, unsigned int&);
    

    Here's what I have so far in .NET (that also doesn't work and says 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'):

    [DllImport(@"C:\test\EQEmuAuthCrypto.dll", EntryPoint = "Encrypt")]
    [return: MarshalAs(UnmanagedType.LPStr)]
    public static extern string Encrypt([MarshalAs(UnmanagedType.LPStr)] string buffer, uint size, ref UInt32 mode);
    

    Here's where I call the function:

    string pwd = "asdf12345";
    UInt32 mode = 5;
    string pwd_enc = EncryptLibrary.Encrypt(pwd, (uint)pwd.Length, ref mode);
    

    Any ideas?

    Thanks!

    Wednesday, August 4, 2010 7:42 PM
  • Just start a new thread in the C# forums, rather than add to an old VB.Net thread. You're getting a similar error message, but the problems are different.

    You are passing a fixed-length char buffer to C to be filled in on return. Look at the "Default marshaling for strings" topic in the MSDN, under "Fixed length String Buffer" - you need to use a StringBuilder.

    Saturday, August 7, 2010 12:52 AM
  • I am working on CRM Document Core pack Templates where i need to use MS Word programming [VBA] to call an External DLL with some credentials but every time i use the following line it throughs me with "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." error message. i don't have any clue why this is happening.

     

    The fragment of my code is as follows:

    'function to return all image files form the record GUID
    Function GetAllImagesFromGUID(ByVal GUID As String) As String()
    'On Error GoTo Err
        Dim outPutFiles() As String
        Dim RecordID As String
        Dim i As Long
        Dim FileType As String
        Dim fileSize As Long
       
        If DBconn Is Nothing Then DBconn_open

        Dim rsPubs As ADODB.Recordset
        Dim CmdRecords As New ADODB.Command
       
        RecordID = GUID
        Set rsPubs = New ADODB.Recordset
        Set CmdRecords = New ADODB.Command
       
        CmdRecords.ActiveConnection = DBconn
        CmdRecords.CommandText = "GetAllImagesFromGUID"
        CmdRecords.CommandType = adCmdStoredProc
       
        CmdRecords.Parameters.Refresh
        CmdRecords.Parameters(1).Value = GUID

        With rsPubs
            .ActiveConnection = DBconn
            .Open CmdRecords
        End With
       
        Dim RecordCount As Integer
        'getting the total number of the records
        Do While Not rsPubs.EOF
        RecordCount = RecordCount + 1
          rsPubs.MoveNext
        Loop
        ReDim outPutFiles(RecordCount - 1, 4)
       
       
        If RecordCount > 0 Then
        Dim RetVal As Double
         'last Modified by Bidhan Chakraborty on 31/01/2011 to avoid unnecessary Path Not Found Error
        Dim curUserProfile As String
        Dim Folders() As String
        Dim TempDirectory As String
       
        TempDirectory = Environ("USERPROFILE")
        Folders = Split(FILEPATH, "\")
       
        Dim index As Long
        index = 0
       
       
        For index = LBound(Folders) To UBound(Folders)
           If Folders(index) <> "" Then
           TempDirectory = TempDirectory & "\" & Folders(index)
           'checking whether the folder already exist or not
           If Dir(TempDirectory, vbDirectory) = "" Then
           MkDir TempDirectory
           End If
           End If
        Next index
         If Dir(TempDirectory & "\" & MatterTITLE, vbDirectory) = "" Then
        MkDir TempDirectory & "\" & MatterTITLE
        End If
         
        Dim FinalPath As String
        FinalPath = TempDirectory & "\" & MatterTITLE & "\"
        FinalPath = Replace(FinalPath, "\", "\\")
        On Error Resume Next
        Dim strTempArument As String
        strTempArument = RecordID & " " & DBSERVERNAME & " " & GOrgNAME & "_MSCRM importer importer"
        RetVal = Shell("" & DLLPATH & "attachmenthandler.exe " & strTempArument & """ & FinalPath & """, vbMinimizedNoFocus)
        'RetVal = Shell("" + DLLPATH + "attachmenthandler.exe " & RecordID & " " & DBSERVERNAME & " " & GOrgNAME & "_MSCRM importer importer """ & FinalPath & """", vbMinimizedNoFocus)
       On Error Resume Next
            i = 0
            rsPubs.Requery
           
            Do While Not rsPubs.EOF
                 If IsNull(rsPubs("mimetype")) = False Then
                   If LCase(Left(CStr(rsPubs("mimetype")), 5)) = "image" Then
                        outPutFiles(i, 0) = CStr(rsPubs("filename"))
                        outPutFiles(i, 1) = TempDirectory + MatterTITLE + "\" + CStr(rsPubs("filename"))
                        i = i + 1
                   Else
                   FileType = Mid(CStr(rsPubs("mimetype")), InStr(CStr(rsPubs("mimetype")), "/") + 1) 'getting the 'application name' from Mimetype field value
                        outPutFiles(i, 0) = CStr(rsPubs("filename"))
                        outPutFiles(i, 1) = TempDirectory + MatterTITLE + "\" + CStr(rsPubs("filename"))
                         If FileType = "msword" Then
                         outPutFiles(i, 2) = "Word.Document.8"
                         outPutFiles(i, 3) = DLLPATH + "All_Icons\wordicon.exe" 'AcroExch.Document.7
                         ElseIf FileType = "pdf" Then
                            outPutFiles(i, 2) = "AcroExch.Document.7"
                            outPutFiles(i, 3) = DLLPATH + "All_Icons\PDFFile_8.ico"
                        ElseIf FileType = "x-ms-wma" Then
                            outPutFiles(i, 2) = "Package"
                            outPutFiles(i, 3) = DLLPATH + "All_Icons\packager.ico"
                         ElseIf FileType = "mpeg" Then
                            outPutFiles(i, 2) = "Package"
                            outPutFiles(i, 3) = DLLPATH + "All_Icons\packager.ico"
                         Else
                         outPutFiles(i, 2) = "Package" ' FileType + ".Application"
                         outPutFiles(i, 3) = DLLPATH + "All_Icons\packager.exe" ' "C:\WINDOWS\system32\packager.exe"
                         End If
                          outPutFiles(i, 4) = FileType
                        i = i + 1
                   End If
                 End If
                rsPubs.MoveNext
            Loop
       End If
        GetAllImagesFromGUID = outPutFiles
        Set rsPubs = Nothing
        'end of Bidhan Modification on 31/01/2011
        Exit Function
    Ext_Function:
      GetAllImagesFromGUID = outPutFiles
    Err:
        MsgBox ("Error occured in GetAllImagesFromGUID function. Please contact your system administrator. Error Message:" + Err.Description)
    End Function

     

    Any help would be highly appreciated.

    thanks in advance

     

    Tuesday, February 1, 2011 1:20 AM