I am trying to create an array of pointers to memory blocks. Below code works if I have only one record. Pretty sure GC is claiming the allocated block so when I go to read the array pointer it is no longer valid. Is there something I can do to make sure the allocated memory does not get claimed? I have tried fixed and keepalive but it does not help. If there is another way to do the below loop please let me know.
byte[] myByteArray;
int NumRec = dsPath.Tables[0].Rows.Count;
IntPtr[] ArrayPnt = new IntPtr[NumRec];
for (int xy = 0;xy < NumRec;xy++)
{
myByteArray = Convert.FromBase64String(dsPath.Tables [0].Rows[xy]["plate"].ToString());
IntPtr Buffer = Marshal.AllocHGlobal(myByteArray.Length);
Marshal.Copy(myByteArray,0,(IntPtr) Buffer,myByteArray.Length);
ArrayPnt[xy] =(IntPtr) Buffer;
}
byte[] myByteArray;
int NumRec = dsPath.Tables[0].Rows.Count;
IntPtr[] ArrayPnt = new IntPtr[NumRec];
for (int xy = 0;xy < NumRec;xy++)
{
myByteArray = Convert.FromBase64String(dsPath.Tables [0].Rows[xy]["plate"].ToString());
IntPtr Buffer = Marshal.AllocHGlobal(myByteArray.Length);
Marshal.Copy(myByteArray,0,(IntPtr) Buffer,myByteArray.Length);
ArrayPnt[xy] =(IntPtr) Buffer;
}