Why does Matlab crashes between mex calls?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hi,
I have mex files around 15 which are linked to same dll. When I run a script using those mex functions, matlab is getting crashed inconsistently with "Access violation". I think the context between the mex calls is not maintaining properly. What to do in order to avoid that.
Thanks.
Accepted Answer
0 votes
The asnwer is easy: One or more of the called Mex functions copntains a bug. This happens frequently, because C-code is very susceptible, e.g. if it was written and tested under a 32 bit version of the compliler and runs with 64 bit addressing now. Note that an access violation can happens directly inside the code, or even later, if the C code has created invalid Matlab variables.
You have to find out, which C function causes the crashes. Then post the code such that the readers can suggest an improvement.
6 Comments
Both mex and dll are written in cpp. I have compiled the dll in 64 bit version only.
Some mex calls are working fine some times but not all the times. Suppose if there is a bug, then why it is working sometimes.
One more thing is that the whole environment is working properly without any single crash in linux platform.
James Tursa
on 7 Jul 2017
As Jan wrote, memory can be corrupted at one time but the crash doesn't happen until later when the memory gets accessed. It only appears to be working sometimes. Regardless, we can't help you unless we can see the source code.
Jan
on 7 Jul 2017
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
long *p;
plhs[0] = mxDuplicateArray(prhs[0]);
p = (long) mxGetData(plhs[0]);
p[0] = 1;
p[1] = 2;
}
Consider the definition of "long" in C: A signed integer with at least 32 bits. It might have 64 bits also, because this depends on the compiler. If the input is a UINT32, the code works with compilers defining long with 32 bits. But if long has 64 bits, the 2 is written to unreserved memory. Perhaps this memory is not allocated and nobody cares, but it is likely that some important information is overwritten. Sometimes the original data might equal the accidentally written value, this is random. Therefore the code might crash directly, work sometimes or let Matlab crash later, when the overwritten value is used.
Compiled "for 64 bit" or "for linux" is not enough to predict, what was applied exactly. It depends on the "data-type model" of the compiler, see e.g. https://en.wikipedia.org/wiki/64-bit_computing: MSVC is "LLP64", most linux compilers are "LP64".
The only solution is to use strictly defined data types, e.g. uint32_T instead of long for the data. In addition the opposite is true for the pointer arithmetics: size_t, mwSize, mwIndex is secure, while any assumption about the bit-width of the pointer types are dangerous. The C sources must be adjusted to work reliably.
"Working sometimes" is a bad problem with C: This language allows to access the memory directly and there are no checks if the bytes have been allocated before. Avoiding the checks is much faster, but dangerous, if the programmer does not exactly know, what he is doing.
Either ask the author, or if it is possible post the codes here. Maybe only small changes are required, but 15 functions might mean, that it is too much for a small favor in the forum.
okay.
I should not post my code, it's confidential. Is the problem might be in Mex or c++ dll?
José-Luis
on 7 Jul 2017
In C++
std::vector<double> bla = {0.0,0.1};
double IwannaDie = bla[3]; //Adios amigo
It can be very many things. Your best bet is to use a debugger. Every other approach is more or less a waste of time unless it's something obvious like the above.
Prasanna
on 7 Jul 2017
How to debug the dll which is linked to mex. I am able to debug mex but not dll. I have visual studio 2012 installed on my computer and I have the source code of the dll also.
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)