MATLAB: Do I get a segmentation fault on Linux when running an application that calls a C shared library compiled by MATLAB Compiler 4.15 (R2011a)

compilererrorfaultgcclibrarylinuxMATLAB Compilermxcallocpointersegsegmentationshared

I follow the steps below:
Compiler Name version: gcc (Ubuntu 4.3.5-3ubuntu1) 4.3.5
(a) Build C shared library libToySatSDR.so from ToySatDR.m.
(b) Run
mbuild ToySDRDriver.c -L. -lToySDR -I.
which executes fine.
(c) On Linux, running the program results in segmentation fault while returning from MATLAB function.
It appears there is nothing wrong with the code, as the same code compiled with Microsoft Visual Studio runs fine on Windows.

Best Answer

On Linux, you get the segmentation fault because you have not allocated memory for the output pointer of pointers. To resolve the crash, you can declare the output variable as a pointer of pointers and then allocate memory, as below:
mxArray **out;
/*This line is necessary when compiled with GCC compiler on Linux*/
out = mxCalloc(1, sizeof(mxArray*));
/*call compiled function*/
mlfToySatDR(1, out, in1);
/*do other stuff*/
/*dispose of pointers properly*/
mxDestroyArray(out[0]);
mxFree(out);
Please note that Microsoft Visual Studio compiler behaves differently, and does not require the allocation of the pointer of pointers.