I have a very simple program, listed below, that reads a value from a .mat file (data file from Matlab) and prints it. For some reason, I get a segfault error after exiting main () - I can run gdb my_program and go through the whole method, but as soon as main() finishes, I enter some method into the Matlab-related library ( libmwfl.so , depending on libmat.so ) that generates segfault.
I am completely new to C programming, but some people read it, I suspect that I either somehow corrupt the stack or call the destructor twice . However, I do not see any of them in my code - and, as I said, I can easily execute my code with a debugger.
What am I doing wrong here?
#include <stdlib.h> #include <stdio.h> #include <mat.h> int main(int argc, char *argv[]) { double value; MATFile *datafile; datafile = matOpen("test.mat", "r"); mxArray *mxv; mxv = matGetVariable(datafile, "value"); value = *mxGetPr(mxv); mxFree(mxv); matClose(datafile); printf("The value fetched from the .mat file was: %f", value); return 0; }
Tomas lycken
source share