09-10-2013 04:08 PM
Hi
I am trying to create a task handle and pass it to another function (in C), but, I am getting a seg fault when trying to close the task handle. What do I have wrong with the code?
Thanks in advance.
#include <stdio.h>
#include <NIDAQmx.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
TaskHandle start() {
int32 error = 0;
TaskHandle taskHandle = 0;
uInt8 data[1] = { 1 };
uInt8 data_open[1] = { 1 };
uInt8 data_close[1] = { 0 };
char errBuff[2048] = { '\0' };
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateDOChan(taskHandle, "Dev1/port0/line13", "", DAQmx_Val_ChanForAllLines));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk(DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Write Code
/*********************************************/
DAQmxErrChk(DAQmxWriteDigitalLines(taskHandle, 1, 1, 10.0, DAQmx_Val_GroupByChannel, data_open, NULL, NULL));
Error: if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if (DAQmxFailed(error))
printf("DAQmx Error: %s\n", errBuff);
return taskHandle;
}
int stop(TaskHandle taskHandle) {
int32 error = 0;
uInt8 data[1] = { 1 };
uInt8 data_open[1] = { 1 };
uInt8 data_close[1] = { 0 };
char errBuff[2048] = { '\0' };
printf("here1\n");
DAQmxErrChk(DAQmxWriteDigitalLines(taskHandle, 1, 1, 10.0, DAQmx_Val_GroupByChannel, data_close, NULL, NULL));
printf("here2\n");
Error: if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if (DAQmxFailed(error))
printf("DAQmx Error: %s\n", errBuff);
return 0;
}
int main(void) {
printf("Press enter to turn the HVPS on.\n");
getchar();
TaskHandle taskHandle = start();
printf("Press enter to turn the HVPS off.\n");
getchar();
stop(taskHandle);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}
09-11-2013 03:31 AM
Not sure whether it's because of you declare taskHandle only in start function? As I know, if you don't declare it in main program, you should not be able to pass it to another function.
09-12-2013 05:17 PM
Hi dchaddock,
TaskHandle is an opaque pointer, and DAQmxClearTask() frees the memory that it points to, so your start() function returns a dangling pointer.
Brad
09-12-2013 05:27 PM
Thanks. Got it sorted out.