06-12-2008 10:32 PM
EXPORT long build_spline(unsigned long *handle, TD1Hdl mag_array, double sigmas[],and gdb shows:
double dielectrics[], LStrHandle error)
{
int i, j, m, n; double g;
VecDoub a, b; MatDoub c;
m = (*mag_array)->dimSizes[0];n = (*mag_array)->dimSizes[1];
a = NRvector<double>(m, sigmas); b = NRvector<double>(n, dielectrics);
c = NRmatrix<double>(m, n, (*mag_array)->elt);
Spline2D_interp *Spline2D = new Spline2D_interp(a, b, c);
g = Spline2D->interp(0.01, 6.0); // sanity check
*handle = (unsigned long) Spline2D;
return 0;
}
EXPORT long eval_spline(unsigned long *handle, double sigma, double dielectric,
double *result, LStrHandle error)
{
Spline2D_interp *Spline2D;
if (*handle == 0) {LV_str_cp(error, (char*) "NULL handle passed to eval_spline\n");return -2;}
Spline2D = (Spline2D_interp*) *handle;
*result = Spline2D->interp(0.01, 6.0);
// *result = Spline2D->interp(sigma, dielectric);
return 0;
}
50 *handle = (unsigned long) Spline2D;
(gdb) print g
$1 = -6.3409099999999965
(gdb) print Spline2D
$2 = (Spline2D_interp *) 0x9509690
(gdb) print *Spline2D
$3 = {m = 7, n = 10, y = @0xff89ad18, x1 = @0xff89ad2c, yv = {nn = 7,
v = 0x9501de0}, srp = {nn = 7, v = 0x95096c8}}
(gdb) del 1
(gdb) cont
Continuing.
Breakpoint 2, eval_spline (handle=0x942b14c, sigma=0, dielectric=5.5,
result=0x942b134, error=0xf4156814) at lookup.cc:62
62 *result = Spline2D->interp(0.01, 6.0);
(gdb) print Spline2D
$4 = (Spline2D_interp *) 0x9509690
(gdb) print *Spline2D
$5 = {m = 7, n = 10, y = @0xff89ad18, x1 = @0xff89ad2c, yv = {nn = 7,
v = 0x9501de0}, srp = {nn = 7, v = 0x95096c8}}
(gdb) cont
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0xf73382a6 in Spline_interp::sety2 (this=0xff89acec, xv=0x3f847ae1,
yv=0x9501de0, yp1=9.9999999999999997e+98, ypn=9.9999999999999997e+98)
at /home/danny/src/NR_C301/code/interp_1d.h:192
192 sig=(xv[i]-xv[i-1])/(xv[i+1]-xv[i-1]);
(gdb) quit
06-13-2008 02:24 AM
06-13-2008 03:54 AM - edited 06-13-2008 03:59 AM
It is a LabVIEW question, more precisely how to write an external code shared library that LabVIEW can interface to. But my C++ knowledge is not good enough to be of much help here.
@StevenA wrote:Is this a LabVIEW question? Try positing this to the Measurement Studio for VC++ board
06-13-2008 07:08 AM
06-13-2008 08:23 AM
06-13-2008 01:48 PM - edited 06-13-2008 01:51 PM
Yes you misunderstood at first what I was trying to say. This here is more precisely what I had in mind but which I might have not been able to express as clearly as I would have wanted to do. LabVIEW does not interfere with memory you allocate yourself at all but it considers handles it passes to the DLL to be generally owned by itself and will accordingly do with them as it pleases as soon as your DLL function returns control to LabVIEw. So storing a reference to a LabVIEW handle passed in as function parameter into a structure to use later will be doomed to fail.
@dgholstein wrote:
Rolfk; I just re-read your post, you indicate that it isn't the data allocated in the DLL that is suspect, that it may be C++ code is storing pointers to the LabView data, which is then clobbered between calls. I'll try building/allocating a new structure and copy all the necessary data into that, then build the splines and work from that. ...I'll let you know
06-13-2008 02:33 PM
06-13-2008 03:44 PM - edited 06-13-2008 03:46 PM
When I said LabVIEW handles I meant parameters like your TD1Hdl one. This is a pointer to a pointer to a memory area where LabVIEW stores the array data (and its dimension sizes). Such handles need to be allocated, resized and finally deallocated using LabVIEW memory manager functions.
@dgholstein wrote:
Rolfk;
Again, thanks for the input -- these are not easy things to program and I don't work with anyone who programs at the same level. An acquaintence at a national lab, who is not familiar with LabView but has programmed similar problems with Python, was able to point out the same things as you did.
One interesting difference between what you describe and how I program. I've been allocating to a pointer, then return that pointer to LabView as an unsigned int, I then typecast that to a reference (for the correct looking wires) and do the opposite when I need to pass it back to my DLL (which I C/C++ typecast back to a C/C++ pointer). I was told that I shouldn't use LabView handles since those are very well-defined for file handles and TCP connections etcetera -- and that I might hurt myself.
BTW: I bundled all the information into one C++ structure and use that pointer (typecast to reference) within LabView, it seems to work now.
...Dan
06-17-2008 05:18 PM
06-17-2008 07:51 PM
typedef struct LV_spline_ptr {allocation is:
int m, n;
double *sigmas; // x1
double *dielectrics; // x2
double *vals; // values
VecDoub a, b; // NR Vectors
MatDoub c; // NR Matix
Spline2D_interp *Spline2D; // NR Spline object
} ;
LV_spline_ptr *ptr = new LV_spline_ptr;and data is returned to LabView with:
*handle = (unsigned long) ptr;Unless the program is a one shot program, destructors need to be written and used to avoid a memory leak.