S-Function && MS Visual Studio C/C++: Segmenation Violation -> how to find error?
Show older comments
Hi,
how can I show the location of a Segmentation violation when coding a C S-Function with MS Visual Studio 2010 (and Matlab R2010b). If I run the code in debugging mode and the problem occurs, Visual Studio stops the run with the message "Matlab.exe has triggered a breakpoint", but it is unable to show the source code and thereby the location of the error. It displays the disassembly instead. When I continue the run, Matlab closes down with the "Matlab System Error" Window and the message "Segmentation violation detected".
In a previous bug of my code, Visual Studio was able to show the source code, which made debugging easier.
Anyone has a hint on this?
Cheers,
m
Accepted Answer
More Answers (4)
Jarrod Rivituso
on 22 Apr 2011
0 votes
I've never heard of anything like this. However, before I knew about debugging S-functions properly, I used to use printf statements everywhere to see how far the code got. I know it isn't ideal, but I made it work in the past.
I think if this problem persists you should contact MathWorks technical support and inform them of the issue.
Good luck!
Kaustubha Govind
on 22 Apr 2011
0 votes
It is likely that the SegV is getting caught in a MathWorks binary (does MSVC show you a stack trace that points to a line number in your S-function?).
Have you tried setting a breakpoint in your S-function and stepping through it line-by-line? This could help you finding the exact line that is causing the SegV.
Uday M
on 22 Apr 2011
0 votes
If you are using the right C MEX S-Function debugging mechanism, as listed in the following tech-note, http://www.mathworks.com/support/tech-notes/1800/1819.html, you should be able to execute the Simulink model containing the S-Function, which should in turn allow you to step through your S-Function code.
However, if you are unable to diagnose the issue even after following the steps listed in tech-note, it may be useful to contact MathWorks technical support.
MIchael
on 20 May 2011
3 Comments
Kaustubha Govind
on 20 May 2011
If you are able to post your S-function code, someone might be able to point out errors.
MIchael
on 21 May 2011
Kaustubha Govind
on 21 May 2011
Ah! Then you might want to go over your code carefully to see if you're not handling memory correctly. Besides allocations and de-allocations, you also must check that you are not reading out-of-bounds memory. Also, if you are work vectors in your S-function, ensure that you are using them correctly.
Finally, if you are able to post the stack trace, that might point to some clues about where the segv is occurring.
Categories
Find more on Startup and Shutdown 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!