Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had a code which was working fine. Suddenly it started giving fatal error at one location. The error message is below. It occurs are a byte[] allocation.

message is
The runtime has encountered a fatal error. The address of the error was at 0x5807ee89, on thread 0x2188. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack


What I have tried:

C#
string[] _GetGPIODIVal=new string[8];
byte[] DataOutBuffer = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
byte[] getBitmodeval = new byte[1];                  
byte c;                   
byte[] DataINBuffer = new byte[1];

for (int i = 0; i < DataOutBuffer.Length; i++)                   
{
c = (byte)(getBitmodeval[0] & DataOutBuffer[i]); //Error occured at this line                       
_GetGPIODIVal[i] = c.ToString();
}
Posted
Updated 25-Feb-16 18:23pm
v2
Comments
Richard MacCutchan 26-Feb-16 5:55am    
Use your debugger to check the value of your array index etc.
Andrea Simonassi 26-Feb-16 6:22am    
In my opinion the error cannot be in the code you posted, that code cannot corrupt the managed heap; maybe other parts of your code is using marshaling, pinvoke or unsafe code, you should look there.
Ghanshyam Nimbalkar 29-Feb-16 3:26am    
This is my function:-

public unsafe void GETGPIODIValues(uint PortHandler, string[] _GetGPIODIVal, int Portnumber)
{
try
{

Array.Clear(_GetGPIODIVal, 0, _GetGPIODIVal.Length);

UInt32 amountInRxQueue = 0;
UInt32 amountInTxQueue = 0;
UInt32 eventStatus = 0;
uint dwNumBytesSentforread = 0;

byte[] DataOutBuffer = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

byte[] getBitmodeval = new byte[1];

byte c;

byte[] DataINBuffer = new byte[1];

fixed (byte* p = DataINBuffer)
{
IntPtr PtrInbuffer = (IntPtr)p;

if (FT_GetStatus(PortHandler, ref amountInRxQueue, ref amountInTxQueue, ref eventStatus) == FTD2XX_NET.FTDI.FT_STATUS.FT_OK)
{
ftStatus = FT_GetBitMode(PortHandler, getBitmodeval); // Get port current status

if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
//oErrorLog.WriteErrorLog("Failed to Read Pin values in Getbitmode." + ftStatus.ToString(), "Fail", "Line NUmber:- " + (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber().ToString(), "Function NAme:- " + "GETGPIODIValues");
Form1.Statusflags = false;
return;
}

//Read Data From port

ftStatus = FT_Read(PortHandler, PtrInbuffer, 1, ref dwNumBytesSentforread);

if (ftStatus == FTD2XX_NET.FTDI.FT_STATUS.FT_OK && 1 == dwNumBytesSentforread)
{
for (int i = 0; i < DataOutBuffer.Length; i++)
{
c = (byte)(getBitmodeval[0] & DataOutBuffer[i]);

_GetGPIODIVal[i] = c.ToString();
}
}
else
{
//oErrorLog.WriteErrorLog("Failed to Read Pin values." + ftStatus.ToString(), "Fail", "Line NUmber:- " + (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber().ToString(), "Function NAme:- " + "GETGPIODIValues");
Form1.Statusflags = false;
return;
}
}
}

Array.Clear(DataINBuffer, 0, DataINBuffer.Length);
Array.Clear(getBitmodeval, 0, getBitmodeval.Length);
Array.Clear(DataOutBuffer, 0, DataOutBuffer.Length);

////oErrorLog.WriteErrorLog("Read GPIO Pin valuesSuccessfully.", "Pass","","");

Form1.Statusflags = true;

}

catch (AccessViolationException ex1)
{
//oErrorLog.WriteErrorLog(ex1.ToString(), "Fail", "Line NUmber:- " + (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber().ToString(), "Function NAme:- " + "GETGPIODIValues");
Form1.Statusflags = false;
return;
}

catch (Exception ex)
{
//oErrorLog.WriteErrorLog(ex.ToString(), "Fail", "Line NUmber:- " + (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber().ToString(), "Function NAme:- " + "GETGPIODIValues");
Form1.Statusflags = false;
return;
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900