CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2005
    Posts
    102

    read/write memory error

    I have an application which gives me the following error when i run it:

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

    But the weird thing is, is that sometimes my program runs perfectly without any error, and sometimes it throws that errot to me. Is there anyway i can prevent this from happening?

    I have no idea why this error shows up...

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: read/write memory error

    How could we possibly answer this without seeing your code? Sounds like you are calling a native method and passing in some bad data (or the native method is doing something incorrectly).

  3. #3
    Join Date
    Nov 2005
    Posts
    102

    Re: read/write memory error

    Well i was hoping there was some general fix like when you get a cross-thread error. Than delegate function mostly solve the issue.
    But here's my code:

    Code:
            void renderLoop()
            {
                while (doLoop)
                {                
                    Application.DoEvents();
                                    
                    //Draw de stencils (als ze zijn enabled) naar de scenery
                    //GEEFT WEL FPS DROP!!!
                    if (Map.Environment.ShadowsEnabled == true)
                    {
                        scenery.FinalizeShadows();
                    }
    
                    foreach (KeyValuePair<int, Character> controller in this.worldController)
                    {
                        controller.Value.doMovement();
                    }
                    this.setCamera();
    
                    lock (this.avatar)
                    {
                        foreach (KeyValuePair<int, TVActor> actor in this.avatar)
                        {
                            actor.Value.Render();
                        }
                    }
    
                    RenderWaterSurfaces();
    
                    //Clear screen
                    engine.Clear();
    
                    //Render map naar scherm.
                    Loader.RenderAll();
    
                    this.engine.RenderToScreen();
                }
    It gives an error at Loader.RenderAll();
    This function is defined inside a DLL which i added as a reference to my project.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: read/write memory error

    Quote Originally Posted by vivendi View Post
    Well i was hoping there was some general fix like when you get a cross-thread error.
    You did not get an error due to cross thread control access, the error was "Attempted to read or write protected memory. Since I have no idea what that RenderAll() method is doing I still cannot help.

    Also, calling DoEvents is really bad practice in .NET and should almost never be necessary.

  5. #5
    Join Date
    Nov 2005
    Posts
    102

    Re: read/write memory error

    I know what the error was, i was just giving an example.
    But i fixed it with locking a few objects.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: read/write memory error

    OK, but irrelevant and incorrect examples are just confusing.

  7. #7
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: read/write memory error

    Quote Originally Posted by BigEd781 View Post
    How could we possibly answer this without seeing your code? Sounds like you are calling a native method and passing in some bad data (or the native method is doing something incorrectly).
    I have a question here. Even I have a c# application which calls a function in a DLL and I get this error whenever I call that DLL function which basically will get me a pointer to a structure which has data of the extracted code.

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


    So I have following questions :
    1) Does it mean that always the problem wil be on the DLL side where this function in defined for this memory erro to occur ?
    2) or as you say even if I pass bad data from my side(C# application side) , it can happen ?

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: read/write memory error

    You can pass in bad data. You could call things with multiple threads when the library only supports a single thread. The library itself can pass bad data or do bad threading. It's hard to tell exactly who's at fault unless you know exactly what caused the error.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: read/write memory error

    Like mUtant_Fruit said, it is hard to tell. For example, a simple cause may be the native method writing past the bounds of an array that you passed to it.

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: read/write memory error

    thanks for the reply.
    In my case I am not passing any OUT variable for the native method to write for it instead it returns me a pointer to a BIG structure as return type ( IntPtr) which I marshal in my side.

    First time it works fine suppose if i call that function for the second time within the same process of my application it throws that error - i guess it is problem with memory on the dll side where they have shortage of heap memory or not cleaning up the things when i call the native method for the second time.

  11. #11
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: read/write memory error

    Quote Originally Posted by Mutant_Fruit View Post
    You can pass in bad data. You could call things with multiple threads when the library only supports a single thread. The library itself can pass bad data or do bad threading. It's hard to tell exactly who's at fault unless you know exactly what caused the error.
    you mean to say the Dll supports only a single thread. If thats the case any idea how to make it support multiple threads ??

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: read/write memory error

    The .NET mantra is "Nothing is threadsafe by default unless explicitly stated". Does your library explicitly state that it is multithread safe? Are you using it in a multithreaded manner? Could this be why it's breaking?

    i guess it is problem with memory on the dll side where they have shortage of heap memory or not cleaning up the things when i call the native method for the second time
    Have you read the documentation for this method? Maybe you're supposed to call a cleanup method explicitly before calling it again. Then again, I'd consider any .NET library which passes around IntPtrs publicly a very badly designed library Also, are you on a 32bit or 64bit OS and is the library designed to cope with whichever it is you have?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  13. #13
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: read/write memory error

    Quote Originally Posted by Mutant_Fruit View Post
    Does your library explicitly state that it is multithread safe?
    No idea. It is developed using VC++ using MS .NET 2005/2008 ?. I am calling that extern function defined in the dll.

    [DllImport("DLL")]
    public extern static IntPtr
    GetFileHead
    (
    [MarshalAs(UnmanagedType.LPStr)]String SourceFolderPath,
    [MarshalAs(UnmanagedType.LPStr)]String PreProcessorPath,
    [MarshalAs(UnmanagedType.LPStr)]String TempFolderPath,
    [MarshalAs(UnmanagedType.LPStr)]String HeaderFilesPath
    );


    Quote Originally Posted by Mutant_Fruit View Post
    Are you using it in a multithreaded manner? Could this be why it's breaking?
    hmmm.... multi thread in the sense I am creating a worker thread before each time i call this API... because it takes more time for this function to return back so that mean while my C# application would not freeze... stopping me from performing any other operations.




    Quote Originally Posted by Mutant_Fruit View Post
    Have you read the documentation for this method? Maybe you're supposed to call a cleanup method explicitly before calling it again.
    yeah I know what exactly that function does. It basically parse all the input c/.h source code and give me the pointer to file structure which has entire parsed code of each file.
    In fact whenever I call this native method they themselves are checking for the pointer and trying to free them before sending me the data so that they wont come across any memory shortage or any other problems.
    Anyways we are planning to call one more API ( native call) from my C# application side as soon as i copy the data from there pointer returned from the first native call GetFileHead

    Quote Originally Posted by Mutant_Fruit View Post
    Then again, I'd consider any .NET library which passes around IntPtrs publicly a very badly designed library Also, are you on a 32bit or 64bit OS and is the library designed to cope with whichever it is you have?
    then what do you suggest to avoid this ? we are going for pointers because it contains HUGE data( its parsed source code) ...
    I am on Windows XP dont know the version properly :-))
    reagrding library i think it works many times only sometime upon repeated call of GETFILEHEAD it crashes...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured