I have a very simple program, listed below, which reads a value from a .mat file (a 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 step through the entire method, but as soon as main() finishes, I enter some method in a Matlab related library (libmwfl.so, a dependency of libmat.so) which throws a segfault.
I am completely new to C programming, but some reading up I suspect that I'm either somehow corrupting the stack or calling some destructor twice. However, I can't see any of those in my code - and as I said, I can step through my code with the debugger without problems.
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;
}