mex-file crashes MATLAB in for loop

3 vues (au cours des 30 derniers jours)
Michael Stollenwerk
Michael Stollenwerk le 9 Août 2020
When I call the mex function (which was created using my c code below) in a for loop, it crashes Matlab after several runs. Do you see anything that is wrong with my code?
Additional Info:
  • The Arb library depends on the GMP, MPFR and FLINT c libraries.
  • I installed a recent version of mingw64 compiler via msys2.
  • I ran setenv('MW_MINGW64_LOC', 'C:\msys64\mingw64') to get the mex command to work.
  • Disclaimer: What I did to create the c code and get the mex command to run was extremely heuristic and trial-and-error-like, since I don't know much about C, compilers, etc.
#include <mex.h>
#include <matrix.h>
#include "arb.h"
#include "arb_hypgeom.h"
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *a_double;
double *b_double;
double *z_double;
double *prec_double;
a_double = mxGetDoubles(prhs[0]);
b_double = mxGetDoubles(prhs[1]);
z_double = mxGetDoubles(prhs[2]);
prec_double = mxGetDoubles(prhs[3]);
/* arb_t represents an interval [m±r][mr,m+r] */
arb_t a, b, z, r;
arb_init(a);
arb_init(b);
arb_init(z);
arb_init(r);
arb_set_d(a, *a_double);
arb_set_d(b, *b_double);
arb_set_d(z, *z_double);
/* Set precision parameter*/
slong prec;
prec = *prec_double;
/* THIS IS THE ONLY FUNCTION I ACTUALLY WANT FROM ARB LIBRARY TO USE IN MATLAB.
(It corresponds to the kummerU function in Matlab) */
arb_hypgeom_u(r, a, b, z, prec);
char *y;
/* This converts the interval arb_t, to a string which is just the midpoint */
y = arb_get_str(r,1000,ARB_STR_NO_RADIUS);
/* output the string y */
plhs[0] = mxCreateString(y);
mxFree(a_double);
mxFree(b_double);
mxFree(z_double);
mxFree(prec_double);
arb_clear(a);
arb_clear(b);
arb_clear(z);
arb_clear(r);
flint_cleanup();
return;
}

Réponse acceptée

Bruno Luong
Bruno Luong le 9 Août 2020
Those are wrong, you should not Free the pointers that you are not Malloc.
mxFree(a_double);
mxFree(b_double);
mxFree(z_double);
mxFree(prec_double);
  1 commentaire
Michael Stollenwerk
Michael Stollenwerk le 10 Août 2020
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by