NullReferenceException in Marshall::Copy 
Author Message
 NullReferenceException in Marshall::Copy

Hello all,

   I' have been banging my head at the following code:

String *npt_ws::npt_array(double (*data) __gc[], int cols, int rows, int
                          num, double th, double cth, double ef, double v) {
    IntPtr datapointer = Marshal::AllocHGlobal(rows*cols); // (1)
    Marshal::Copy((*data), 0, datapointer, (cols*rows)); // (2)
    double *pdatapointer = (double *) (datapointer.ToPointer());
    char *result = npt_main_array(pdatapointer, cols, rows, 2, 0.0, 0.0,
                                  0.0, 0.0);
    String *resultString = Marshal::PtrToStringAnsi(result);
    delete result;
    return result;


   The code is being called from C#. The array "data" is being filled in
that C# code. I have checked that there actually is real data in "data"
so I'm sure that's fine. The variables "rows" and "cols" specify the
size of that array (my spiel is to treat arrays as one-dimensional
   What this code is supposed to do is to pass the data into some unmanaged
C code (called thru npt_main_array(...)). As you can see in the code, in
line (1) I'm allocating memory for an unmanaged data structure. In line (2),
I'm Marshall::Copy'ing the data into that unmanaged data structure. Or so
I hope. The problem is, that I get an error in line (2) - which seems to
depend on the size of the array "data" (on rows*cols). For very small values
of "rows" and "cols" (say, 2 and 10) things work fine. However, for larger
values they don't. In line (2) the code bombs out and the error message
says something like that there is a NUllReferenceException. Which I don't
understand because when I add a test before line (2) to see whether
"datapointer" is NULL or not I get that it isn't. So it seems that both
"data" and "datapointer" are fine and, yet, the code bombs out. Does anybody
have any idea what the problem is?

Thanks, Joerg

Sat, 08 Oct 2005 01:06:52 GMT  
 [ 1 post ] 

 Relevant Pages 

1. How to marshall c-callback functions through COM?

2. Book FS: Understanding DCOM (w/ sealed CD) by by William Rubin, Marshall Brain

3. Howto Marshall an Object (or an Interface to an OBject)

4. Book FA: Understanding DCOM (w/CD) by William Rubin, Marshall Brain

5. How to marshall interfaces of COleClientItem-Clones?

6. How to marshall interfaces of COleClientItem-Clones?

7. How to marshall?

8. How do I marshall HINTERNET

9. Problems Allocating Simple Array - NullReferenceException

10. NullReferenceException : Object reference not set to an instance of an object

11. System.NullReferenceException

12. Simple System.NullReferenceException question


Powered by phpBB® Forum Software