08-19-2009 10:32 AM - edited 08-19-2009 10:35 AM
Ok, I tried to find how to solve memory leak problem due to task handlers in this forum but almost every body says that I have call DAQmxStopTask and DAQmxClearTask but there should be something to deal with the task process, and I figured it out how to solve memory leak problem. So I want to share it with any newbie programmer like me. I know there are many - countless great programmers who already knew how to solve this, but this post is for just a noob like me.
The situation is, I compiled codes and it never returns any error, runs fine and even displays a right data in VC++ program but found that it eats up memory up to 256 MB within 5 min! It started eating memory from 27 MB at the first run and 10 min later it makes the whole system stall. My system checks digital ports status and uses many task handlers even before user clicks any button. These task handlers must be unloaded from the memory after task is done.
Here is a part of my code;
DAQmxErrChk (DAQmxCreateTask("",&AHandle));
DAQmxErrChk (DAQmxCreateAOVoltageChan(AHandle, "Dev1/ao0","",minVal,maxVal,DAQmx_Val_Volts,""));
DAQmxErrChk (DAQmxWriteAnalogF64(AHandle,numSampsPerChan,1,0.0,DAQmx_Val_GroupByChannel,&double_CurrentVoltValue,&sampsPerChanWritten,NULL));UnreserveTheResources(AHandle);
UnreserveTheResources(AHandle) is receiving a task handler AHandle and calls these functions;
void UnreserveTheResources(TaskHandle TaskHandler)
{
int32 error = 0;
DAQmxErrChk (DAQmxWaitUntilTaskDone(TaskHandler, 0));
DAQmxErrChk (DAQmxTaskControl(TaskHandler, DAQmx_Val_Task_Stop));
DAQmxErrChk (DAQmxTaskControl(TaskHandler, DAQmx_Val_Task_Abort));
DAQmxErrChk (DAQmxTaskControl(TaskHandler, DAQmx_Val_Task_Unreserve));
DAQmxErrChk (DAQmxClearTask(TaskHandler));
TaskHandler = 0;
Error:
if(error)
CleanUp(error, TaskHandler);
return;
}
Now my system only eats 27 MB maximum. I hope this little sharing helps your work.
Regards,