LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem allocating memory in a DLL

Hi

I've written a DLL that exports a couple of functions. The first one
allocates a LabView array using DSSetHandleSize, as below:

typedef struct {
int32 dimSizes[2];
unsigned char data[1];
} LABVIEW_2D_BYTE_ARRAY;
typedef LABVIEW_2D_BYTE_ARRAY **LABVIEW_2D_BYTE_ARRAY_HANDLE;

....

error = DSSetHandleSize(lhDiffArray, sizeof(int32)*2+
dim1*dim0*sizeof(unsigned char));
if (error != noErr)
{
*pError = LG_ERROR_MEMORY;
return FALSE;
}
(*lhDiffArray)->dimSizes[1] = dim1;
(*lhDiffArray)->dimSizes[0] = dim0;

The second function takes a LabView array handle and fills the array
with some data. My VI is simple - I have a call library function node
which calls the first function, the array output is wi
red to the input
of a second call library function node which calls the second function
and the array output is wired to a 2D intensity graph control. All
functions accept and return the array as a handle to a 2D array of
unsigned 8 bit ints.

Now, this all works fine, and I can run it as many times as I like.
However, if I run it twice (or more), it causes LabView to crash when
I close the VI (presumably as my DLL is unloaded). The application
error always says that the program tried to write to address
0x00000010. If I remove the graph control, the problem goes away.

Is there something I should be doing to ensure everything gets put
away tidily when the VI closes?

Cheers

mark-r

--
I put ten different puns in my .sig
I hoped that at least one of them would make you laugh
No pun in ten did
0 Kudos
Message 1 of 2
(2,577 Views)
> I've written a DLL that exports a couple of functions. The first one
> allocates a LabView array using DSSetHandleSize, as below:
>


I'm not exactly sure what is going wrong, but this takes place in many
drivers DLL access functions, analysis functions, etc.

So, the first call resizes the array to the size you need. The second
dereferences and fills in the data. To debug what is going on, you
might want to selectively leave out the calls, replacing them with
built-in functions and see if it becomes obvious.

The first is equivalent to the Reshape Array node or calling
SetCINArraySize() or NumericArrayResize() -- at least I think those are
the names. You can also use a for loop and Replace Array Element to
fill in elements. You might also be able
to increase the size of the
array so that an illegal access will immediately crash instead of
corrupting memory and leading to a later crash. By allocating several
MB, the allocation will come straight from the system and be far removed
from the others.

Greg McKaskle
0 Kudos
Message 2 of 2
(2,576 Views)