Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Pointers

Status
Not open for further replies.

JRudisill

Programmer
Aug 4, 2003
54
0
0
US
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;
}
 
The Buffer allocated in that way is not collected until you call Marshal.FreeHGlobal(Buffer);
You must call the FreeHGlobal on ArrayPnt when you finish with it.
I think the error is somewhere else.
1)Use try-catch to capture the error or :
Code:
string s = Marshal.PtrToStringAuto(ArrayPnt[xy]);
to see what string is there before and after.
obislavu


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top