10-27-2014 11:56 AM
Hello,
I am facing a memory leak issue when I execute a dll created using CVI to read a XML file.
Each iteration of the step is taking around 200k of memory.
Short description of the code:
Basically I am using a function created in CVI to read from an XML file by tag which 2 attributes: command and the response;
int GetCmdAndRsp(char XML_File[MAX_STR_SIZE], char tag[MAX_STR_SIZE], char Command[MAX_STR_SIZE], char Response[MAX_STR_SIZE], char ErrorDescription[MAX_STR_SIZE])
inputs:
- XML_File_path;
- tagToFind;
ouputs:
- Command;
- Response;
- Error;
Example:
XMLFile:
...
<WriteParameter Command="0x9 %i %i %i %i %i" Response = "0x8 V %i %i %i %i"/>
...
Execution:
error = GetCmdAndRsp("c:\\temp\\ACS_Messages.xml" ,"WriteParameter", cmd, rsp, errStr)
output:
error = 0
cmd = "0x9 %i %i %i %i %i"
rsp = "0x8 V %i %i %i %i"
errStr = "Unkown Error"
Everything is working correctly but I have this memory leak issue. Why am I having such memory consumption?? Is it a TestStand or CVI issue??
Each iteration I am loading the file, reading the file and discarding the file.
Attached you can find the CVI project, a TestStand sequence to test (ReadXML_test2.seq) and an example of a XML file I am using.
Please help me here.
Thaks in advance.
Regards,
Pedro Moreira
10-28-2014
10:53 AM
- last edited on
04-22-2025
01:29 PM
by
Content Cleaner
Pedro,
I think memory leak is in your CVI code.
In multiple functions, your code does memory allocation and deallocation as follows:
char *tag = NULL;
...
tag = calloc (1, length + 1);
...
tag = NULL;
...
free (tag);
You are basically passing NULL for the free function. According to https://www.ni.com/docs/en-US/bundle/labwindows-cvi/page/cvi/libref/cvifree.htm if NULL is passed to free function, no action will be performed.
You should first call free function and then assign NULL to pointer. So, your code should look like:
char *tag = NULL;
...
tag = calloc (1, length + 1);
...
free (tag);
tag = NULL;
- Shashidhar
10-29-2014 04:13 AM
Hello Shashidhar,
Thanks for the help.
But unfortunetly this didn't solve my issue.
Even with your the changes you propose I observed the following:
First iteration:
After 100 iterations:
Do you have any other suggestion?
Thanks.
10-30-2014
05:02 AM
- last edited on
04-22-2025
01:30 PM
by
Content Cleaner
Pedro,
When a TestStand step executes, its result will be stored by TestStand which will be later used for generating reports or logging data into database.
You are looking at the memory (private bytes) when the sequence file has not finished execution. So, the memory you are looking at, includes the memory used by TestStand to store result of the step. The memory used for storing results will be de-allocated after finishing the sequence file execution.
Hence, we dont know if there is actual memory leak or not. You should look at the memory, before and after executing sequence file instead of looking in between execution.
Also, here are some pointers that will be helpful for checking memory leak in an application:
1. TestStand is based on COM and uses BSTR in many function. BSTR caches the memory and because of the behavior, sometime you might get false notion of having memory leak. Hence, you need to use SetOaNoCache function OR set the OANOCACHE=1 environment variable to disable caching.
2. Execute the sequence file atleast once before doing the actual memory leak test. The dry run will make sure all static variables are initialized before doing memory leak test.
3. Make sure that the state of system or application is same when considering the Private bytes. Ex: Lets say ReportViewControl is not visible before you start executing sequence file. Then you note down the private bytes and then execute the sequence file. After finishing execution, make sure you close the ReportViewControl and then note down the private bytes once again to check if memory is leaked or not.
4. If there exists memory leak as you specified, it is possible that the leak is either in TestStand, or in your code. Make sure that your code doesn't leak by creating a small standalone application (probably a console application) which calls your code.
Detecting memory leaks in CVI is better explained in
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA03q000000x1joCAA&l=en-US
- Shashidhar