We interacted with the library created from Matlab Compiler. Our problem is with the array returned from the library.
Once we are done with the array, we want to free up memory, however this leads to random segmentation errors.
Here is the Matlab library ( bugtest.m ):
function x = bugtest(y) x = y.^2;
Here is the command we used to create it (creating libbugtest.so and libbugtest.h ):
mcc -v -W lib:libbugtest -T link:lib bugtest.m
Here is our C testing program ( bug_destroyarray.c ):
#include <stdio.h> #include <stdlib.h> #include "mclmcrrt.h" #include "libbugtest.h" #define TESTS 15000 int main(int argc, char **argv) { const char *opts[] = {"-nojvm", "-singleCompThread"}; mclInitializeApplication(opts, 2); libbugtestInitialize(); mxArray *output; mxArray *input; double *data; bool result; int count; for (count = 0; count < TESTS; count++) { input = mxCreateDoubleMatrix(4, 1, mxREAL); data = mxGetPr(input); data[0] = 0.5; data[1] = 0.2; data[2] = 0.2; data[3] = 0.1; output = NULL; result = mlfBugtest(1, &output, input); if (result) { /* HERE IS THE PROBLEMATIC LINE */ /*mxDestroyArray(output);*/ } mxDestroyArray(input); } libbugtestTerminate(); mclTerminateApplication(); }
This is how we compile the C program (create bug_destroyarray ):
mbuild -v bug_destroyarray.c libbugtest.so
We believe mxDestroyArray(output) is problematic.
To verify the failure, we run the following:
- On each of 32 cluster nodes.
- Run
bug_destroyarray . - Monitor output for segmentation errors.
In approximately 10% of cases, a failure occurs. If this is independent by nodes, then you can assume that it falls by about 0.3% of the time.
When we take out this problematic line, we cannot cause it to crash.
However, memory usage gradually increases when this line is not included.
From our research, it seems that we should not destroy the returned array, if not, how do we stop the memory leak?
Thanks.