C++ Mex File Crashing Intermittently

I am compiling and using a C++ mex function in Matlab 2011b.
The mex file is acting very strangely. About half of the time it crashes while the other half it does not crash and runs all of the way through.
Does anyone have any ideas what would cause a Mex file to work intermittently that is called from the same place and the same way?
Thanks,
Kevin

 Akzeptierte Antwort

Geoff
Geoff am 16 Mär. 2012

1 Stimme

Sounds to me like you're doing something dodgey inside the C++ code. Specifically, a buffer overrun or invalid pointer access. These have a habit of causing intermittent problems. If your code is not too lengthy or commercially sensitive, feel free to post it.

4 Kommentare

Kevin
Kevin am 16 Mär. 2012
Can you explain a bit how a coding problem would be intermittent? It seems odd since most codes either work or they don't.
Geoff
Geoff am 16 Mär. 2012
Yes, if you are accessing memory that does not belong to your program, you never know what it might contain.
It might be unused and not cause problems (unless you expect it to contain a valid pointer, perhaps).
But the next time you call, you might just happen to stray into memory that is owned by another process. That is not allowed.
I am just conjecturing here, assuming the most probable cause.
Uninitialised values are another biggie here. Your code might run fine because usually an important (but uninitialised) variable happens to be zero. But next time you run, your program is loaded into another part of memory, and your variable happens to assume a value like 1187126605. If this was an index into an array or string, you'd almost certainly crash.
Kevin
Kevin am 16 Mär. 2012
This is exactly what happened. Thanks Geoff.
Geoff
Geoff am 19 Mär. 2012
Hehe, I thought so =) No worries, and glad you got it sorted.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 16 Mär. 2012

0 Stimmen

It is impossible to guess the reason without seeing the code.
You can use a debugger to find the problem. Even some print commands allow to find the line, which causes the crash.

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 16 Mär. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by