Click to See Complete Forum and Search --> : read/write memory error
vivendi
October 4th, 2009, 02:59 PM
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...
BigEd781
October 4th, 2009, 03:02 PM
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).
vivendi
October 4th, 2009, 03:19 PM
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:
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.
BigEd781
October 4th, 2009, 03:22 PM
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.
vivendi
October 4th, 2009, 04:55 PM
I know what the error was, i was just giving an example.
But i fixed it with locking a few objects.
BigEd781
October 4th, 2009, 05:24 PM
OK, but irrelevant and incorrect examples are just confusing.
vcdebugger
October 5th, 2009, 06:48 AM
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 ?
Mutant_Fruit
October 5th, 2009, 07:25 AM
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.
BigEd781
October 5th, 2009, 12:44 PM
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.
vcdebugger
October 5th, 2009, 11:56 PM
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.
vcdebugger
October 5th, 2009, 11:59 PM
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 ??
Mutant_Fruit
October 6th, 2009, 03:57 AM
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?
vcdebugger
October 6th, 2009, 04:10 AM
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
);
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.
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
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...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.