LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pass data as reference from dll to Labview

Dear Labview developers, 

My application holds some larger blocks of data inside a dll in the heap. When Labview wants to access the data, it gets copied to a properly resized Labview-handle. In order to improve performance and avoid the copying I wonder if the following is possible:

I would like to move the data from the dll heap to the Labview memory space. When Labview requests some data, it doesn’t have to be copied, but instead a handle to the data is returned to Labview as reference. I already figured out how to do this with a “Call Library Node“ configured with “adapt to type” and “pointers to handles”.

In the dll it looks like this: 

 

//data handle owned by the dll

TD1Hdl myData;

 

.

.

.

 

//hand over data handle to labview

 void getData(TD1Hdl *pHandle)

{

    *pHandle = myData;

} 

 

My problem is that Labview thinks it now owns the handle and therefore deletes the memory associated with the handle after usage. Is there a way to tell Labview not to do so? Maybe some kind of reference counting or a mechanism to lock memory?

Thanks in advance for your help!
0 Kudos
Message 1 of 2
(3,213 Views)

Xerxess wrote:

Dear Labview developers, 

My application holds some larger blocks of data inside a dll in the heap. When Labview wants to access the data, it gets copied to a properly resized Labview-handle. In order to improve performance and avoid the copying I wonder if the following is possible:

I would like to move the data from the dll heap to the Labview memory space. When Labview requests some data, it doesn’t have to be copied, but instead a handle to the data is returned to Labview as reference. I already figured out how to do this with a “Call Library Node“ configured with “adapt to type” and “pointers to handles”.

In the dll it looks like this: 

 

//data handle owned by the dll

TD1Hdl myData;

 

.

.

.

 

//hand over data handle to labview

 void getData(TD1Hdl *pHandle)

{

    *pHandle = myData;

} 

 

My problem is that Labview thinks it now owns the handle and therefore deletes the memory associated with the handle after usage. Is there a way to tell Labview not to do so? Maybe some kind of reference counting or a mechanism to lock memory?

Thanks in advance for your help!

No!

 

First: this will only work wthout crash if you use the DSNewHandle() and similar functions to allocate the memory handle. These LabVIEW memory manager functions need to be called in order to allocate LabVIEW handles that LabVIEW can deal with.

 

Second:unlike C where the caller has to normally provide memory buffers and also manages them, LabVIEW handles are owned by whomever operates on them at the moment. Passing a handle back to a caller passes control of that handle to the caller. This is mandatory due to the dataflow paradigma in LabVIEW. If two sinks consume a memory handle to modify it, dataflow requires to make copies. There is no way around that that I would know of. Maybe (likely) that LabVIEW variants do emply ref counting but the C manager API to operate on them is undocumented and therefore a no go.

On the LabVIEW diagram LabVIEW can and will optimize the calling order of nodes to schedule nodes that do not reuse a buffer before nodes that will reuse a buffer to minimize data copies. This is based on internal knowledge of LabVIEW which functions can reuse buffers, and will modify them or not. No such control is possible to external C code in a DLL or CIN since LabVIEW can not know what that function will do with a handle and must assume the worst.

 

There is a trick I have used in the past but that will only work if you do not really need to use the data in the handle after the fucntion returns. Basically I return the handle to the caller (needs handles to be passed by reference) and NULL out the internal handle. When the internal (asynchronous) function gets around to fill in new data it does recognize the NULL handle and allocates a new appropriate handle before filling it in. Otherwise it just resizes it. Obviously this needs protection or at least synchronization somehow to avoid handing the handle to LabVIEw at the moment the check is done in the asynchronous function. You could just to a multithreading safe exchange of the passed in handle with the handle in your internal structure. Still needs to do the NULL handle check in the asynchronous function since empty handles passed by reference into a C function will be just a NULL value (why allocate 8 bytes to store a pointer and a 0 length) when a NULL can mean the same).

 

Rolf Kalbermatter

Message Edited by rolfk on 02-05-2009 09:06 AM
Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 2
(3,113 Views)