Why does Matlab crashes between mex calls?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
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
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.
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.
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?
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.
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
