calllib crashes Matlab... no error given

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

Sounds like a segmentation fault. Do you assume, that somebody can give a useful advice without knowing any details?
"char gnFn[10][1024];" is C, EVAL is MATLAB. Do you try to run C-code by using EVAL?!
I am running the following...
[notfound, warnings] = loadlibrary('MyLib.dll','MyLib.h');
When I check warnings, I get the warning I placed in my EDIT above. I also get an additional warning displayed to the command line as "Warning: The data type 'int8#' used by structure gnMod does not exist. The structure may not be usable."
Orginally, I didn't notice the warnings and proceeded to use calllib and that crashed my Matlab session. I didn't get an error on the crash and not crash files were saved, Matlab just closed. After this happened, I went back to check what I was doing and that is when I noticed the loadlibrary warnings. This is the first time I've tried to use dll's in Matlab, so I don't understand the errors/warnings. Specifically, I can't anything describing the warning I posted above. What does it mean? It looks like Matlab doesn't like the "char gnFn[10][1024]" line inside a structure in my MyLib.h file, but the coding is (looks) correct. I'm lost!
Have you run "mex -setup" before calling loadlibrary? If yes, what compiler did you select?
Yes, and I selected Lcc. It is currently the only compiler installed on the machine I am doing this work.
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"
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).
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
The dll I am using was written in C and it is for 32-bit Windows. I am using 32-bit Windows XP and Matlab 7.11.0 (R010b) at the moment, so there should be no problems there. The error/warning reads as if Matlab (or the compiler?) is reading some of the C code in my header file incorrectly. Is that what you are tying to get at Kaustubha?
@Chirag - The call to calllib I am using is the following.
>> calllib('MyLib','Func1','test.dat',10)
MyLib is the library (dll). Func1 is one function in the library and it takes as input, a data file 'test.dat' and an integer id number. Here that id number is 10.
I tried using the Visual Studio Express C/C++ compiler, but I still get the same error. To be sure, I ran 'mex -setup' and selected the MS compiler instead of LCC. I believe this is the proper way to switch the compiler.
Can you call using:
>> calllib('MyLib','Func1','test.dat',int32(10))
if integer is an int32!
I will try that and report back, but I don't know if it will help. I think calllib may not be my problem. I think there are issues with loading the library in the first place. I just noticed that loadlibrary requires Perl. I didn't think about whether I had Perl on the machine I'm working, because I'm always use to having it (I use Linux mostly, but I am using Windows at the moment). I don't have Perl on this machine, so I'm installing it right now.
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]
@Philip - Is there a verion of Matlab that does support two dimensional arrays like mine? Does Matlab have documentation about not supporting two such arrays? If I didn't write the dll or the associated header file, is there a way for me to still use the dll?
Any advice or suggestions?
Chris
Chris on 27 Nov 2024
Edited: Chris on 27 Nov 2024
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

Sign in to comment.

 Accepted Answer

tlawren
tlawren on 2 Nov 2011
I discovered the cause of my problem. The dll I am using requires a specific license and I didn't have that. The documentation provided by the creators of the dll's don't say anything about licensing, so it took some C code and phone calls to figure things out.
I should note that the Matlab errors/warnings I received seem to make no sense with respect to my license issue. Once I set my license files up correctly, everything ran seamlessly in Matlab!

1 Comment

I have the same issue as yours. But what I want matlab to do is to pop up a window saying license file missing, instead of crashing and without giving a proper error. I tried using try and catch but matlab still crashes. do you have any suggestion?

Sign in to comment.

More Answers (1)

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.
This link gives one possible solution: 1-671ZZL
Another is to modify the c header file to properly state the calling convention and rebuild if needed.

3 Comments

I tried the solution from the link and it did get rid of one of the warnings I was getting, but calling calllib still causes Matlab to exit without a stack trace. Loading the library with a prototype file where instances of cdecl are changed to stdcall gets rid of the following warning.
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.
This is the warning returned from loadlibrary in
[nofile,warning] = loadlibrary(...);
loadlibrary still spits 'int8#' warning to the command line. I'm curious why one warning goes to the warning output and a second warning goes to the command line. Could the command line warning be associated with some function call inside of loadlibrary?
The two warnings I've been seeing are one-in-the-same, at least I pretty sure they are. When I examined the prototype file that was generated by loadlibrary, I can see that 'int8#' is associated with the structure field gnFn. I'm guessing Matlab tries to parse char gnFn[10][1024], but doesn't know what to do with it so it just stops at 'int8#'. To be sure, if I remove the # symbol in the prototype file and then re-load the library, I don't get any warnings at all. This is wrong though, because the structure in the header still calls for a multi-dimensional array. Nonethelss, the calllib crash still occurs with this change, so it is likely not the cause of the crash, as you pointed out. Moreover, the function I'm trying to call in my dll doesn't even use this structure, so that also leads me to believe that it isn't behind the crash.
Could a third calling convention other than cdecl and stdcall be used in my dll?
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.

Sign in to comment.

Asked:

on 14 Oct 2011

Edited:

on 27 Nov 2024

Community Treasure Hunt

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

Start Hunting!