The best practice is to create and destroy tasks outside of structures like For Loops or While Loops to avoid memory problems altogether. You can see this in action by looking through the shipping examples in the NI Example Finder
You can also let the NI-DAQmx driver manage its own de-allocation by enabling auto cleanup on the DAQmx Create Task.vi
. If the auto cleanup parameter is set to true, the resources allocated in task creation will be freed when the VI completes. If auto cleanup is set to false, the resources will be freed when LabVIEW exits.