Mex file access violation

5 views (last 30 days)
Takaaki Itoga
Takaaki Itoga on 22 Jan 2016
Commented: Takaaki Itoga on 26 Jan 2016
I wrote a mex file to call a Fortran subroutine from MATLAB, and access violation occurs. Probably the reason for this error is that I make some mistakes when trying to passing the output variable y from Fortran to MATLAB. I insert mxPrintf into the program and can see where it stops, but I do not know how to correct it. I rewrote the code with a very simple subroutine, built it, and the problem persists when running it. I am sorry for asking such a basic question, and thank you in advance.
#include "fintrf.h"
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
implicit none
mwPointer plhs(*),prhs(*)
integer nlhs,nrhs
mwPointer mxCreateNumericArray,mxGetNumberOfElements,mxGetPr,x_pr
mwPointer y_pr
mwSize mxGetNumberOfDimensions,dims(2)
integer mxClassIDFromClassName,classid
double precision x(2),y(2)
call mexPrintf("a")
dims(1)=2
dims(2)=1
call mexPrintf("b")
classid=mxClassIdFromClassName('double')
call mexPrintf("c")
plhs(1)=mxCreateNumericArray(2,dims,classid,0)
call mexPrintf("d")
x_pr=mxGetPr(prhs(1))
call mexPrintf("e")
y_pr=mxGetPr(plhs(1))
call mexPrintf("f")
call mxCopyPtrToReal8(x_pr,x,mxGetNumberOfElements(x_pr))
call mexPrintf("g")
call test(x,y)
call mexPrintf("h") ! Write after this line, the program stops.
call mxCopyReal8ToPtr(y,y_pr,mxGetNumberOfElements(y_pr))
call mexPrintf("i")
return
end
subroutine test(x,y)
implicit none
double precision x(2),y(2)
y(1)=1
y(2)=2
return
end
  1 Comment
Takaaki Itoga
Takaaki Itoga on 22 Jan 2016
I solve the problem by myself. I should have put prhs(1) and plhs(1) in the parentheses following mxGetNumberOfElements.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 25 Jan 2016
Note that the signature for mxCopyPtrToReal8 is as follows:
subroutine mxCopyPtrToReal8(px, y, n)
mwPointer px
real*8 y(n)
mwSize n
And likewise:
subroutine mxCopyReal8ToPtr(y, px, n)
real*8 y(n)
mwPointer px
mwSize n
So the last agrument is mwSize, not mwPointer. This may cause problems for you in the future if you are on a system where mwSize does not match mwPointer. You might consider writing code that forces the correct type. E.g.,
mwSize n
:
n = mxGetNumberOfElements(prhs(1))
call mxCopyPtrToReal8(x_pr,x,n)
:
n = mxGetNumberOfElements(plhs(1))
call mxCopyReal8ToPtr(y,y_pr,n)
And in Fortran, it is usually a good idea to always use variables for arguments to the API routines so you can force the correct type being passed. E.g., I never pass literal integers in arguments, I always create a variable of the desired type, set it to the desired value, and then pass that as the argument.
  1 Comment
Takaaki Itoga
Takaaki Itoga on 26 Jan 2016
Thank you very much for your answer. I noticed the problem you point out after submitting my question. I will follow your advice to avoid any possible future problems.

Sign in to comment.

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!