LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

memory leak with callback function

Solved!
Go to solution

Hi,

I am fairly new to LabWindows and the ninetv library, i have mostly been working with LabVIEW.

I am trying to create a basic (no GUI) c++ client that sets up subscriptions to several network variables publishing DAQ data from a PXI.

The data for each variable is sent in a cluster and contains various datatypes along with a large int16 2D array for the data acquired(average array size is 100k in total, and the average time between data sent is 10ms). I have on average 10 of these DAQ variables.

I am passing the same callback function as an arguement to all of these subscriptions(CNVCreateSubcription).

It reads all the correct data, but i have one problem which is that i am experiencing a memory leak in the callback function that i pass to the CNVCreateSubscription.

I have reduced the code one by one line and found the function that actually causes the memory leak, which is a CNVGetStructFields(). At this point in the program the data has still not been passed to the clients variables.

 

This is a simplified version of the callback function, where i just unpack the cluster and get the data (only showing from one field in the cluster in the example, also not showing the decleration).

The function is passed into to the subscribe function, like so:

 

static void CNVCALLBACK SubscriberCallback(void * handle, CNVData data, void * callbackData);

 

CNVCreateSubscriber (url.c_str(), SubscriberCallback, NULL, 0, CNVWaitForever, 0 , &subscriber);

 

 

static void CNVCALLBACK SubscriberCallback(void * handle, CNVData data, void * callbackData)
{


int16_t daqValue[100000];

long unsigned int nDims;
long unsigned int daqDims[2];
CNVData fields[1];
CNVDataType type;
unsigned short int numFields;

 

CNVGetDataType(data, &type, &nDims);
CNVGetNumberOfStructFields (data, &numFields);
CNVGetStructFields (data, fields, numFields); // <-------HERE IS THE PROBLEM, i can comment out the code after this point and it still causes a memory leak.

CNVGetDataType(fields[0], &type, &nDims);

CNVGetArrayDataDimensions(fields[0], nDims, acqDims);
CNVGetArrayDataValue(fields[0], type, daqValue, daqDims[0]*daqDims[1]);

CNVDisposeData(data);

 

 

At the average settings i use all my systems memory (4GB) within one hour. 

 

My question is, have any else experienced this and what could the problem/solution to this be?

 

Thanks.

0 Kudos
Message 1 of 4
(4,599 Views)
Solution
Accepted by topic author soeien

As far as I can see you only clear the memory of the handle (data) but not the memory of the structure; hence before calling CNVDispose(data) you should have something like

 

while (numFields > 0)
  CNVDisposeData (fields[--numFields]);

Message 2 of 4
(4,597 Views)

Of course.....if it is something i hate more than mistakes, it is obvious mistakes.

Thank you for pointing it out, now everything works 🙂

0 Kudos
Message 3 of 4
(4,572 Views)

Glad to hear that everything is working - be assured, the next issue will show up soon Smiley Wink

0 Kudos
Message 4 of 4
(4,562 Views)