I think the issue was by ths following , Dereferencing a pointer requires two things: The memory referred to by the pointer must be allocated at the time of the dereference. Any dereference to an offset from the pointer must be less than the size of the allocated memory.
it should still fix some issue I was getting the exact same message in an application, and it was very hard to get it solved. I have googled for that message and have found a number of different causes and solutions.... none of them worked for me. Well, in my case, I was using a COM object in my .NET application.... and that error would appear randomly in different parts of the code. Eventually, after lot of time debbugging, I found that the following line was causing the error:
I hope this helps you . Well, I've knocked this one down. As I have suspected, it was because of MVCContrib. Mind that I am using MVC 3 Beta which is not yet officially supported by MVCContrib. With that in mind, I have downloaded the latest MVCContrib sources for MVC 3 branch. Go to MVCContrib Sources, switch to the mvc3 branch, download, and build the binaries with the attached bat. Then, include the needed files into your solution.
Marshal.StructureToPtr throw "Attempted to read or write protected memory" error
Hope this helps Without looking too much at your size calculations, what struck me is that you're passing true to the call to Marshal.StructureToPtr which means it will try to free the unmanaged memory as if it was allocated for the structure specified before marshalling it (and allocating memory for any pointers needed in the structure). Since you have not initialized your unmanaged memory this would likely lead to incorrect pointers being freed. If you pass false to this call, it seems to work just fine. Also if you initialize all the unmanaged memory to zeroes before the call seems to have the same effect, for example:
IntPtr mInfoPtr = Marshal.AllocHGlobal(mInfoSize * 3); //throw error
for (int i = 0; i < mInfoSize * 3; i++)
Marshal.WriteByte(mInfoPtr, i, 0);
Marshal.StructureToPtr(mInfo, mInfoPtr, true);
will help you In my experience, that message was a result of a memory leak. This is what I'd do if I am in your situation especially since you are working on a third-party DLL. 1) Monitor your WCF server and see what is going on with the DLLHost.exe and the aspnet services in the task manager. I have a feeling that your third-party DLL has a memory leak that causes these 2 services to bloat and reach the limit of your servers memory. This is the reason why it works for a while and then suddenly just stopped working.
C# - Marshal.Copy : Attempted to read or write protected memory
Hope this helps You seem to be considering 3 times the stride for every row; your code will only work for the first third of the image; after that you have indeed gone outside of your allowed range. Basically: