- rnorms_p is allocated when (NLHS .EQ. 2), but you release the memory when (NLHS .EQ. 3) - there is probably a typo in one of these conditions (they must be the same condition).
- PLHS(3) is being allocated twice using mxCreateDoubleMatrix when (NLHS .EQ. 2) is false.
compile .mexglx with ifort glib detected error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am trying to compile with ifort 11.1. I have gotten this mex file to compile and run on a Windows machine with a Lahey compiler. Now I would like it to work on my Ubuntu machine.
Here is the mexfunction:
#include <fintrf.h>
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
IMPLICIT NONE
INTEGER PLHS(*), PRHS(*)
INTEGER NLHS, NRHS
INTEGER mxCreateDoubleMatrix,mxMalloc,flag_complex
INTEGER mxIsComplex
INTEGER mxGetPr,mxGetM,mxGetN, mxGetPi,indvec_p, indveci_p
INTEGER A_pr,A_pi, INPUT2_p, T_pr, T_pi, rnorms_p
INTEGER m, n, k, flag_fixed_k
REAL*8 INPUT2(1), acc
c Check that we have two inputs and two or three outputs
IF (NRHS .NE. 2) THEN
CALL mexErrMsgTxt('id_decomp requires two input arguments')
ELSEIF (NLHS .LT. 3) THEN
CALL mexErrMsgTxt('id_decomp requires 2 or 3 output arguments')
ELSEIF (NLHS .GT. 4) THEN
CALL mexErrMsgTxt('id_decomp requires 2 or 3 output arguments')
ENDIF
c Extract the problem dimensions from the first input argument.
m = MXGETM(PRHS(1))
n = MXGETN(PRHS(1))
c Assign a pointer to the second input argument.
INPUT2_p = MXGETPR(PRHS(2))
CALL mxCopyPtrToReal8(INPUT2_p,INPUT2,1)
c Check the nature of INPUT2: c If INPUT2 < 1, then set acc = INPUT2 and use the "specified accuracy" version of id_idd. c If INPUT2 >= 1, then set k = INPUT2 and use the "specified rank" version of id_idd.
IF (INPUT2(1) .LT. 1) THEN
flag_fixed_k = 0
k = min(m,n)
acc = INPUT2(1)
ELSE
flag_fixed_k = 1
k = min(int(INPUT2(1)), min(m,n))
END IF
c Check whether the input matrix A is complex.
flag_complex = mxIsComplex(PRHS(1))
IF (flag_complex .EQ. 0) THEN
CALL mexErrMsgTxt('comp_id_decomp cannot yet handle real input')
END IF
c Create the output matrix T and copy A onto T.
IF (flag_complex .EQ. 1) THEN
A_pr = mxGetPr(PRHS(1))
A_pi = mxGetPi(PRHS(1))
PLHS(1) = mxCreateDoubleMatrix(m,n,0)
T_pr = mxGetPr(PLHS(1))
PLHS(2) = mxCreateDoubleMatrix(m,n,0)
T_pi = mxGetPr(PLHS(2))
CALL mxCopyPtrToReal8(A_pr,%VAL(T_pr),m*n)
CALL mxCopyPtrToReal8(A_pi,%VAL(T_pi),m*n)
END IF
c Create the vector "rnorms". c If there are only 2 output arguments, then it is c created simply as a temporary array, otherwise it c is created as an output.
IF (NLHS .EQ. 2) THEN
rnorms_p = mxMalloc(8*n)
ELSE
PLHS(3) = mxCreateDoubleMatrix(1,n,0)
rnorms_p = mxGetPr(PLHS(3))
ENDIF
c Create an array for the index vector. This is only c a temporary work array since the actual output has c to consist of reals to follow matlab standards.
indveci_p = mxMalloc(4*n)
c Do the actual computations.
CALL id_pass(%VAL(T_pr),%VAL(T_pi),acc, m,n,k, 1 %VAL(indveci_p),%VAL(rnorms_p),flag_fixed_k)
CALL mxSetM(PLHS(1),k)
CALL mxSetN(PLHS(1),n-k)
CALL mxSetM(PLHS(2),k)
CALL mxSetN(PLHS(2),n-k)
c Create the REAL VALUED output index vector indvec.
c Then copy the integer vector indveci to the real valued output indvec.
PLHS(3) = mxCreateDoubleMatrix(1,n,0)
indvec_p = mxGetPr(PLHS(3))
CALL COPY_IND2REAL(%VAL(indveci_p), %VAL(indvec_p), n)
c Free the temporary work arrays.
CALL mxFree(indveci_p)
IF (NLHS .EQ. 3) THEN
CALL mxFree(rnorms_p)
ENDIF
RETURN
END
It compiles both in Matlab and in the terminal but both ways lead to the following error.
glibc detected * /usr/local/bin/glnx86/MATLAB: double free or corruption (!prev): 0x0a0f39e0 *
I don't know where the problem may be. Maybe flags? Or missing libraries?
Any help is much appreciated.
Thanks.
0 comentarios
Respuesta aceptada
Kaustubha Govind
el 10 de Mzo. de 2011
From the error message, it seems like you have a memory leak in your function (from my experience, Linux is more robust at detecting runtime memory corruption, than Windows - this could be why you don't see any error on Windows).
I see at least two instances of memory leaks in your file:
Más respuestas (0)
Ver también
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!