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

  • Question

  • Hi All,

    I have wrapper DLL build in VC++ and C# Windows application.

    My Windows application imports the DLL functions and use them. One of the DLL function requires an address to function in the windows application. Actually the implementation is like an activity monitor. Once I pass this function(will work as a Callback function from DLL) to the DLL, it will use that as a callback function address to raise it from DLL to notify my windows application about some event. Sometime when DLL calls this callback function from VC++ it gives me that error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt. System.AccessViolationException".

    I am passing a reference to one static structure object which contains the callback object. Before trying with static object this error was occurring very frequently but after changing parameters to static its frequency has been reduced.

    I googled and tried unchecking JIT option and other stuffs but nothing is working for me.

    Can anybody help me getting out of this problem?

    I appreciate your time and inputs.

    Thanks,
    Jigs
    • Moved by liurong luo Wednesday, December 2, 2009 9:52 AM off-topic issue. (From:Visual C# General)
    Wednesday, November 25, 2009 1:44 PM

Answers

  • Hello Jigs,

    Thanks for your feedback! Since it is a trouble shoot issue, I'm going to move it to the off-topic forum if you don't mind. But since we have found that the issue occurs in the VC++ dll file, do you have the source code? Please try to post threads in Visual Studio Debugger forum. Also, to troubel shoot the issue in the dll file, I recommend you read How to Debug Dlls for more information.

    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Edited by liurong luo Wednesday, December 2, 2009 9:52 AM link
    • Marked as answer by liurong luo Wednesday, December 2, 2009 9:52 AM
    Wednesday, December 2, 2009 9:51 AM

All replies

  • Hello Jigs,

    I suggest you debug the project and find out where the exception occurs:
     1. press F5 to debug, when the exception occurs, open the Call Stack window (Where we could get the current call stack)
     2. set a breakpoint at the begining of the method (which was got from Call Stack window)
     3. Get the breakpoint hit and then Step through the code (press F10)
    If needed, please enable unmanaged debugging (Property page -> Debug -> Enable Debuggers)
    After finding where the exception occurs, we may have an idea on how to fix it. You could also paste the code snippet here.

    Please have a try and let me know the result!
    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Friday, November 27, 2009 11:29 AM
  • Hello Roahn Luo,

    Thanks for your reply.

    I tested by putting debug points but it is not the code in c# which generates error. I believe it is the vc++ dll code which tries to call the callback function and throws the error (I guess because it is not finding the callback function in memory may be disposed). In my c# application it just shows isolated AccessViolationException error information box. It also does not contain any detail information.

    Thanks,
    Jigs
    Monday, November 30, 2009 4:41 AM
  • Hello Jigs,

    Thanks for your feedback! Since it is a trouble shoot issue, I'm going to move it to the off-topic forum if you don't mind. But since we have found that the issue occurs in the VC++ dll file, do you have the source code? Please try to post threads in Visual Studio Debugger forum. Also, to troubel shoot the issue in the dll file, I recommend you read How to Debug Dlls for more information.

    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Edited by liurong luo Wednesday, December 2, 2009 9:52 AM link
    • Marked as answer by liurong luo Wednesday, December 2, 2009 9:52 AM
    Wednesday, December 2, 2009 9:51 AM
  • Hello Roahn Luo,

    I do not think that it is a bug in VC++ DLL. DLL code is just trying to call that function which was supplied to it. I think the callback is getting disposed and DLL is trying to call it. As this bug is random in nature I believe this is the reason that it is giving error.

    Thanks,
    Jigs
    Wednesday, December 2, 2009 10:37 AM