LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Having Issue with Array Resizing

I am working with a LabVIEW-Python interface, using a LabVIEW DLL to perform operations called from Python via the cffi library. Memory allocation occurs before passing the inputs to the DLL, and resizing happens after the DLL processing completes. Both operations are handled within the Python script using LabVIEW's internal Array Resize function.
 
However, resizing the array to 0 elements does not work. When I attempted to use the DSDisposeHandle (DeAllocate) function instead, it caused a crash, which is why I reverted to using the Resize function with a size of 0. I am using LabVIEW 2021 SP1 (64-bit). I want to understand why resizing to 0 fails. Is this behavior intended by design in the LabVIEW memory manager, or am I implementing it incorrectly?
0 Kudos
Message 1 of 11
(400 Views)

Hi Dhatshayini,

 


@Dhatshayini wrote:
am I implementing it incorrectly?

How exactly did you implement "it"? Mind to share some code?

 

The ReshapeArray works as expected in LabVIEW:

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 11
(391 Views)

You need to show what you did. Memory management of LabVIEW data is not just something you can vibe code. This listens very closely and there is more to it than just calling some function randomly.

 


@Dhatshayini wrote:
I want to understand why resizing to 0 fails. Is this behavior intended by design in the LabVIEW memory manager, or am I implementing it incorrectly?

YES the LabVIEW Memory Manager definitely can do this and YES you are implementing something wrong. But without looking at the actual code both for your LabVIEW DLL and the Python FFI interface, we can only say these two things.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 3 of 11
(346 Views)

@Dhatshayini wrote:
I am working with a LabVIEW-Python interface, using a LabVIEW DLL to perform operations called from Python via the cffi library. Memory allocation occurs before passing the inputs to the DLL, and resizing happens after the DLL processing completes. Both operations are handled within the Python script using LabVIEW's internal Array Resize function.

It is not clear at all how your code is layered and what happens where. What parts are graphical and what parts are text based?

 

So you have a DLL built in LabVIEW that you call from python. At the same time, you give conflicting information where the "resizing" happens (inside the DLL as a last step after processing? After the dll in the calling code?). Does the DLL have python nodes? Is the caller again a LabVIEW program using a python node? Something else?

 

As has been said, there is no "internal" (sic) LabVIEW resize function, (except for "matrix resize") just "reshape array" (which can also do other things (trim, pad, change number of dimensions, etc).

 

What's the logical purpose of the resizing?

0 Kudos
Message 4 of 11
(306 Views)

Dhatshayini_0-1780547458021.png

Dhatshayini_1-1780547819203.png

 


I connected the ROI array input to the connector pane of a blank VI containing no internal logic, and then generated the DLL. The C bindings consist of a compiled header file containing the definition of the ROI structure along with its allocate and resize functions. However, when I try to resize the array to 0, the operation fails (I have attached both the header file and the code snippet)

 

0 Kudos
Message 5 of 11
(173 Views)

@Dhatshayini wrote:

(I have attached both the header file and the code snippet)

 


You did not attach anything.

0 Kudos
Message 6 of 11
(165 Views)

Attached both as screenshots

0 Kudos
Message 7 of 11
(155 Views)

Hi Dhatshayini,

 

where does that "roii.dll" come from?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 11
(141 Views)

@Dhatshayini wrote:

Attached both as screenshots


Well, and now anyone who would like to help you will have to recreate the VI and the Python code based on your screenshots from the scratch?!. Could you please attach the VI used as the exported function in the DLL (ideally saved back to LabVIEW 2020, since not everyone has the latest version), the LabVIEW project with the build specification for that DLL, and the Python code as a minimal reproducible example? It’s an unusual and somewhat risky mixture of Python and LabVIEW, but in principle it should work.

Message 9 of 11
(137 Views)

@Dhatshayini wrote:

Attached both as screenshots


So you have an array of structures which contain more arrays of stuff in there and you resize the outer array handle and wonder why it doesn't work the way you think it should?

 

Python does NOT know about LabVIEW handles. Accordingly you can not just use the Python ctypes syntax to threat a LabVIEW handle as a C array. That roi[0][0].dimSize is extremely suspect already. It's however not the main problem you have here at the moment.

 

The problem you have is that if you resize a LabVIEW handle to 0 it is usually deallocated, not just the dimSize changed to 0. Both, a valid handle with dimSize set to 0 or a NULL handle, are valid ways to indicate an empty array in LabVIEW. Your code has to be prepared for that possibiliy. This is a performance optimization that has been in LabVIEW since at least around LabVIEW 3.0, since creating a handle that only contains an int32 to indicate that it contains nothing is quite costly.

 

So your resizing simply deallocates the array and stores a NULL value in the pointer you create with fff.new("RoiArray*", roi). But you then ignore that value and try to access the original roi variable, which is still a pointer value but now pointing to invalid memory. Since there is not a lot of chance that that memory got reused in the meantime, it still contains the previous value of 55 but it is meaningless, since the memory area it is pointing at has been freed by the call to your resize function.

 

Rather than creating every time a new pointer to pass as a reference to the roi handle to the resize function and then just forget about it, you should create that reference value before the function call, pass it to the function and then correctly re-reference the handle by dereferencing that reference after every resize call. Yes handles are double referenced so that the actual handle pointer doesn't change when the handle content itself is resized. But that is only valid if the handle is resized to a value >0. Resizing it to 0 is usually equivalent to deallocating it but if you really want to be sure it is deallocated you need to explicitly call the according deallocate function instead. 

 

Something like this should correctly work:

 

roiptr = ffi.new("RoiArray*", roi)
err = dll.ResizeRoiArray(roiptr, 0)
if (!err)
    roi = roiptr[0]
    if (roi)
        print(roi[0][0].dimSize)
    else
        print("returned a null handle")
else
    print("Resize returned error: ", err)

 

And since your array contains internal arrays, never try to deallocate the array by calling the LabVIEW memory manager function DSDisposeHdl() directly. This function will not know about the contents of the handle and accordingly can't deallocate the contained handles in it. You should call explicitly the DeAllocateRoiArray() function in your DLL, which will make sure any non-NULL contained handles will also be correctly deallocated.

 

And the roi[0][0] syntax to dereferenc the double referenced handle while technically correct is extremely shaky and unsafe. And as you see above, never ever just reference the roi variable like this without checking that it itself is not null. If your AllcateRoiArray() fails it will return a null value, if the ResizeRoiArray() function resizes the array to 0 or fails, it may or may not return a null value in the reference and the DeallocateRoiArray() function for sure always will store a null value in the reference, unless when it fails for some reason midway, which would be however highly unlikely and indicate corrupted memory and from that point on forward anything you will do is unsafe anyways.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 10 of 11
(108 Views)