LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW DLL 2D Array output deallocation

Hey everyone,

I am using a custom built DLL made with LabVIEW.  One of the functions outputs a 2D array of data by "handle".  I can catch the 2D array from the function just fine from within my C program, however I have a question about deallocating the memory allocated by the LabVIEW DLL for the 2D array.

I call the DLL function repeatedly in a loop and as this occurs the RAM used on my machine continues to increase until it is full or I exit the program.  There are no other dynamically allocated peices of data within the C program that could be causing this.

The question is, can I call free() on the 2D array handle safely?  Is this the correct way to deallocate the 2D arrays passed out of LabVIEW DLL functions?  One dimension of the 2D array is always the same size, but the other dimension varies per call.

Here is an example (I have renamed function calls and library headers due to security reasons):

#include "DLLHeader.h"

int main(void)
{
    ResponseArrayHdl myArray = NULL;

    DLL_FUNC_InitializeLibrary();
   
    DLL_FUNC_ConfigurePortWithDefaults("COM3");
   
    DLL_FUNC_StartAutoSend(9999999);
   
    while(DLL_FUNC_IsTransmitting())
    {
        DLL_FUNC_GetAllAvailableResponseMessagesByHndl(&myArray);      // gets the 2D array

        // do something with array data here!
   
        free(myArray);   // is this the appropriate way to clean up the array?

        Delay(0.05);
    }

    DLL_FUNC_GetAllAvailableResponseMessagesByHndl(&myArray);      // gets the 2D array
   
// do something with array data here!
    free(myArray);   // is this the appropriate way to clean up the array?
      
    DLL_FUNC_ShutdownLibrary();

    return 0;
}


from DLLHeader.h:
typedef struct {
   ...
    } ResponseMessage;     
// created by the LabVIEW DLL build process

typedef struct {
    long dimSize;
    ResponseMessage Responses[1];
    } ResponseArray;
typedef ResponseArray **ResponseArrayHdl;   // created by the LabVIEW DLL build process


I want to make sure this is an appropriate technique to deallocate that memory, and if not, what is the appropriate way?  I am using LabVIEW 7.1 if that matters.  The C program is just ANSI C and can be compiled with any ANSI C compliant compiler.

Thanks!
~ Jeramy
CLA, CCVID, Certified Instructor
0 Kudos
Message 1 of 5
(3,374 Views)
Update...when I tested using free() on the handle pointer I received a run-time error: "Attempt to free pointer to memory not allocated by malloc() or calloc()."
CLA, CCVID, Certified Instructor
0 Kudos
Message 2 of 5
(3,370 Views)

Hi Jeramy,

LabVIEW uses "Memory Manager" to alloacte and free memory. Since LabVIEW allocated the array (inside the LabVIEW dll), it is up to the LabVIEW memory manger to deallaocte or free the memory. This might not occur until the dll is unloaded.

To handle the memory management in your C code, you may have to use the LabVIEW manager functions to allocate, free, and resize arrays, strings or other data structures that are passed into or out of your library from LabVIEW.

These functions are declared in the "extcode.h". The extcode.h is the header file for the set of LabVIEW manager functions that performs simple and complex operations including managing memory. To use the extcode.h you must include the "labview.lib" in your project. It is located in  "c:\Programs file\labview7.1\cintools" directory.

A relevant function that will be applicable in this case (to free the memory) is the "AZDisposeHandle or DSDisposeHandle". For more information on this function and other memory management functions, checkout the " Using External Code in LabVIEW" reference manual, chapter 6 (Function Description), Memory Management Functions.

Tunde

Message Edited by Tunde A on 01-09-2007 06:58 PM

0 Kudos
Message 3 of 5
(3,347 Views)


@Tunde A wrote:

A relevant function that will be applicable in this case (to free the memory) is the "AZDisposeHandle or DSDisposeHandle". For more information on this function and other memory management functions, checkout the " Using External Code in LabVIEW" reference manual, chapter 6 (Function Description), Memory Management Functions.

Tunde


One specific extra info. All variable sized LabVIEW data that can be accessed by the diagram or are passed as native DLL function parameters use always DS handles, except the paths which are stored as AZ handles (But for paths you should use the specific FDisposePath function instead).

So in your case you would call DSDisposeHandle on the handle returned by the DLL. But if the array itself is of variable sized contents (strings, arrays, paths) you will have to first go through the entire array and dispose its internal handles before disposing the main array too.

Rolf Kalbermatter

Message Edited by rolfk on 01-10-2007 10:43 PM

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 4 of 5
(3,328 Views)
Thanks! I haven't gotten to try this yet, but it makes alot of sense.  I will also be informing the NI engineer I've been working with...perhaps they should have a knowledgebase article on this.  If they do already have one I couldn't find it via their website search...

Thanks again!
CLA, CCVID, Certified Instructor
0 Kudos
Message 5 of 5
(3,320 Views)