calllib crashes Matlab... no error given
Show older comments
Greetings. I am trying to use some functions in a dll, but when I use calllib, Matlab crashes (and closes) without error.
Any idea what is going on?
EDIT: I just noticed that I got a warning(s) when I loaded the library.
Warning: Eval of const expression 10 ][ 1024 failed with error Unmatched right square bracket at (eval 5) line 1, at end of line syntax error at (eval 5) line 1, near "10 ]" Missing right curly or square bracket at (eval 5) line 2, at end of line
The offending line is...
char gnFn[10][1024];
???
16 Comments
Jan
on 14 Oct 2011
Sounds like a segmentation fault. Do you assume, that somebody can give a useful advice without knowing any details?
Jan
on 14 Oct 2011
"char gnFn[10][1024];" is C, EVAL is MATLAB. Do you try to run C-code by using EVAL?!
tlawren
on 14 Oct 2011
Kaustubha Govind
on 14 Oct 2011
Have you run "mex -setup" before calling loadlibrary? If yes, what compiler did you select?
tlawren
on 14 Oct 2011
Kaustubha Govind
on 14 Oct 2011
LCC is a C compiler - could you verify if you DLL is a C library or C++ library? loadlibrary uses the C compiler selected using "mex -setup" to parse the symbols - it is likely that you will see error with LCC if you are attempting to load a C++ library.
If you're not sure, I would recommend installing one of the MSVC Express (free) editions supported for your MATLAB version (see http://www.mathworks.com/support/compilers/previous_releases.html) and select that with "mex -setup"
Kaustubha Govind
on 14 Oct 2011
Also, make sure that if you're on 64-bit MATLAB, the DLL is also a 64-bit binary (similarly in case if 32-bit).
Chirag Gupta
on 14 Oct 2011
What is the actual call to calllib? What is the exact data that you are passing to the function with calllib? Typically crashes like these are due to the fact that the correct data was not passed (data that MATLAB is available to convert to the appropriate C struct
tlawren
on 14 Oct 2011
tlawren
on 17 Oct 2011
Chirag Gupta
on 17 Oct 2011
Can you call using:
>> calllib('MyLib','Func1','test.dat',int32(10))
if integer is an int32!
tlawren
on 17 Oct 2011
Philip Borghesani
on 17 Oct 2011
MATLAB includes a copy of Perl for its own use on Windows. The warnings you are seeing could cause problems in the future with some calls to the library but are probably not the cause of your current crash. The version of MATLAB you are using does not support two dimensional arrays declared like:
char gnFn[10][1024]
tlawren
on 17 Oct 2011
tlawren
on 18 Oct 2011
While troubleshooting a similar issue, an option we did not have to pursue (solved the issue by using 2019a instead of MATLAB 2024a) was using a parallel process to run the DLL. If the DLL took a certain amount of time (in our case, 1-2 seconds was enough to know there was a problem), then the parallel process could be killed and move on. At least that was the principle. Maybe someone out there could make that a reality for their issue/workaround.
Another part of the problem was that initially my inputs exceded what the DLL could handle. So the DLL could perhaps have been coded better, but that part at least was my error.
See also
Accepted Answer
More Answers (1)
Philip Borghesani
on 17 Oct 2011
1 vote
The usual cause of a calllib call causing MATLAB to exit with no stack trace is the use of the wrong calling convention. loadlibrary defaults to cdecl but many 32 bit libraries are built with stdcall as the default calling convention.
Another is to modify the c header file to properly state the calling convention and rebuild if needed.
3 Comments
tlawren
on 17 Oct 2011
tlawren
on 17 Oct 2011
Philip Borghesani
on 19 Oct 2011
Without more information is unlikely that we can help you. The amount of information needed probably belongs in a tech support call not this area.
Needed information
# Matlab version
# copy of header file
# copy of DLL
# full text/error output from your call to loadlibrary and any calllib calls.
Categories
Find more on Matrix Indexing 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!