matlab crashes without dump file when using fopen for file

4 views (last 30 days)
I am using gnumex with mingw for compiling mex files in matlab in Windows OS. I am not able to use fopen command to open files. Following is the code which I am using.
#include <stdio.h>
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
FILE *fp=NULL;
fp = fopen("test.txt", "w+");
fclose(fp);
}
The program compiles succesfully but when I try to run the compile mexw64 file, matlab simply closes down without generating any dump file.
Note that I am able to run simple Hello world program and passing and receiving arguments using other mex programs.
I cannot debug using http://www.mathworks.com/help/matlab/matlab_external/debugging-on-microsoft-windows-platforms.html as I am compiling using mingw along with gnumex setup in windows
Output of
mex -v file.cpp
This is mex, Copyright 1984-2007 The MathWorks, Inc.
-> Default options filename found in C:\Users\achaudhary\AppData\Roaming\MathWorks\MATLAB\R2009b
----------------------------------------------------------------
-> Options file = C:\Users\achaudhary\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat
MATLAB = C:\PROGRA~1\MATLAB\R2009b
-> COMPILER = gcc
-> Compiler flags:
COMPFLAGS = -c -DMATLAB_MEX_FILE -x c++
OPTIMFLAGS = -O3
DEBUGFLAGS = -g
arguments =
Name switch = -o
-> Pre-linking commands =
-> LINKER = C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\linkmex.pl
-> Link directives:
LINKFLAGS = -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex GM_ISCPP -mwindows
LINKDEBUGFLAGS = -g -Wl,--image-base,0x28000000\n
LINKFLAGSPOST =
Name directive = -o file.mexw64
File link directive =
Lib. link directive =
Rsp file indicator =
-> Resource Compiler = C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\rccompile.pl --unix -o mexversion.res
-> Resource Linker =
----------------------------------------------------------------
--> gcc -c -DMATLAB_MEX_FILE -x c++ -oC:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj -IC:\PROGRA~1\MATLAB\R2009b\extern\include -IC:\PROGRA~1\MATLAB\R2009b\simulink\include -O3 -DMX_COMPAT_32 file.cpp
--> C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\linkmex.pl -o file.mexw64 -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex GM_ISCPP -mwindows -s C:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj
link command: g++ -shared C:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex\mex.def -o file.mexw64 -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex -mwindows -s C:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj -llibmx -llibmex -llibmat
  5 Comments
Philip Borghesani
Philip Borghesani on 13 Feb 2015
Once attached use the g (go) command to resume MATLAB. Then just run the mex file normally from MATLAB. The program should break at the first uncaught exception. It is frequently simpler and faster to debug if MATLAB is started with the -nojvm option to disable all Java support reducing the number of threads and removing Java's use of access violations to manage the heap.
amit chaudhary
amit chaudhary on 14 Feb 2015
Thank you Philip.
Looked at the debugger and found out that mex file was using libraries from cygwin64 from earlier installation as well. To make the previous installation work, I had to remove -mno-cygwin flag from the mexopts.bat generated by gnumex manually as it was incompatible with the installed gcc version. Somehow, everything apart from FILE open worked.
So did a fresh install of mingw64 and appended environment paths accordingly. Then reinstalled gnumex using mingw64 and now everything works fine.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!