If you are using NI TestStand and VISA instrument drivers then you have a possibility to start using NI Session Manager, which help you to close all opened sessions(handles) automatically on the end of the sequence or callbacks (See NI Session Manager help file).
Anyhow, you must use this algorithm:
CheckError(viOpenDefaultRM (&io));
CheckError(viOpen (io, name, VI_NULL, VI_NULL, &vi));
.............
here is your subseq.
or code
.............
CheckError(viClose (io));
CheckError(viClose (vi));
"Only a life lived in the service to others is worth living..." - Albert Einstein