Hi all,
I got a weird problem when using FreeHGlobal. The error message I get
is:
Unhandled Exception: System.NullReferenceException: Object reference not set
to an instance of an object.
at System.Runtime.InteropServices.Marshal.ThrowExcept ionForHR(Int32
errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.FreeHGlobal (IntPtr hglobal)
Here's the code:
// the variables lines and vertices are defined elseplace
IntPtr num_pointer = Marshal::AllocHGlobal(lines*sizeof(int));
IntPtr id_pointer = Marshal::AllocHGlobal(lines*sizeof(int));
IntPtr coord1_pointer = Marshal::AllocHGlobal(vertices*sizeof(double));
// some more of these
// num, id, and coord1 are passed to this code from some other code
Marshal::Copy((*num), 0, num_pointer, lines);
Marshal::Copy((*id), 0, id_pointer, lines);
Marshal::Copy((*coord1), 0, coord1_pointer, vertices);
// some more of these
int *pnum_pointer = (int *) (num_pointer.ToPointer());
int *pid_pointer = (int *) (id_pointer.ToPointer());
double *pcoord1_pointer = (double *) (coord1_pointer.ToPointer());
// some more of these
// some processing (calling unmanaged code...)
Marshal::FreeHGlobal(num_pointer);
Marshal::FreeHGlobal(id_pointer);
Marshal::FreeHGlobal(coord1_pointer);
The last lines give the FreeHGlobals that break. If I comment those
out, the code runs though fine. What am I doing wrong here?
Best,
Joerg Colberg