Why 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)?

12 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Jan 2012
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.

More Answers (0)

Products


Release

R2011a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!