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).
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.
Re: read/write memory error
Quote:
Originally Posted by
vivendi
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.
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.
Re: read/write memory error
OK, but irrelevant and incorrect examples are just confusing.
Re: read/write memory error
Quote:
Originally Posted by
BigEd781
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 ?
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.
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.
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.
Re: read/write memory error
Quote:
Originally Posted by
Mutant_Fruit
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 ??
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?
Quote:
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?
Re: read/write memory error
Quote:
Originally Posted by
Mutant_Fruit
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
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
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
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...