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

Hi,

I am getting this error in my application: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

It is an application developed using Visual C++.Net with Visual Studio 2005 Beta 2 release and it uses Windows Media SDK 9.0 and has a DLL built with mixed code (native and managed). I need to pass a reference of a form to one of the methods written in mixed mode DLL. Form is a managed class.

It works fine first time the method is called. On subsequent occasions it gives above error. If application is restarted it again works for the first time.

Does anyone have a clue why this can happen

Prasad


Answer this question

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

  • Dr. Pizza

    What does the debugger say. If an error is reproducable in this way you can be lucky.
    The reasons are many: Overwritten memory, buffer overrun, unlocked pointer, pointer to a pined managed object were the pn is freed, but the pointer is still in use...

    Repro the error in the debugger and watch the call stack.

  • Dr McAwesome

    Your problem is genuine. And since it is problem , it has a soluton too. The solution lies with you ;only you need to look at it in a different view. I have faced the similar problem and took me weeks to solve it.

    The main cause behind it lies to the highly enhanced security features of .NET framework 2.0 over .NET 1.1. It has imposed several restiction , policies and permission sets for accessing the data from the web application, It uses full trust as well as partial trust.

    1) If you try to access the data you don't have permission too or if the source is not trusted it gives the above exception. You not only have to give permission to your application to access the source BUT you need to make the source as trusted one too.

    2)Also, the other main cause is that , if you try to read the data that is not available, it gives the above exception.

    So its time now to revise your concepts about CAS and permissions in visual studio.NET 2005 which is highly advanced than .NET 1.1.

    For details click the link below http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnnetsec/html/foundstone.asp

    Go thro it docs and decide which permission your application requires and implement it.

    Best of Luck.

    mail me a thank you if you are benefitted by this post.

    Balaji Birajdar

    balaji.birajdar@iopsis.com


  • georgw

    I could locate the problem statement but not the reason.

    In one of the lines in our code we are calling Dispose method of a .Net wrapper class to an unmanged class.

    If I comment out this call the code seems to function as expected.

    The reason is not clear to me. If someone can throw light on this behavior after calling Dispose method this way it would be helpful.

    Thanks.
    Prasad

  • John Malkovich

    There might be more reasons.
    Maybe your destruction code is called more than once, using memory pointer that are already freed to be freed again.

    What happens in the unmanaged class in this moment Is there access via a pointer that has undefined values If so, were is this pointer freed or were the memory gets freed were it points to.



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