{"id":3080,"date":"2013-12-19T23:21:50","date_gmt":"2013-12-20T04:21:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=3080"},"modified":"2013-12-19T23:21:50","modified_gmt":"2013-12-20T04:21:50","slug":"problems-with-your-s-functions-try-this-diagnostic","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/12\/19\/problems-with-your-s-functions-try-this-diagnostic\/","title":{"rendered":"Problems with your S-Functions&#8230; Try this Diagnostic!"},"content":{"rendered":"<p>Ahhh... <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/c-c-s-functions.html\">C Mex S-function<\/a>... <\/p>\r\n\r\n<p>If you ever tried creating a C Mex S-function and were a little rusty on your C programming, you might have experienced strange behavior or event MATLAB crashes. This is possible... C Mex S-Functions let you do almost anything you can think of, including shooting yourself in the foot.<\/p>\r\n\r\n<p>This week I want to highlight a diagnostic that saved me a lot of time when that happened to me: <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/diagnostics-pane-data-validity.html#bq8t6q3-36\">Array Bounds Exceeded<\/a>.<\/p>\r\n\r\n<p><strong>The Problem<\/strong><\/p>\r\n\r\n<p>Every once in a while, I receive models with one or many of the following symptoms:<\/p>\r\n\r\n<ul>\r\n\t<li>Sporadic, seemingly random, crashes resulting in a dialog saying <strong>MATLAB has encountered an internal problem and needs to close<\/strong><\/li>\r\n\t<li>Inexplicable wrong results for some signals<\/li>\r\n\t<li>MATLAB crashes without any error, it just disappears<\/li>\r\n\t<li>Different behavior on different machines (e.g. crash on machine A, wrong results on machine B, different wrong results on machine C, etc...)<\/li>\r\n<\/ul>\r\n\r\n<p>S-functions with bugs in them can cause that type of behavior. To help debugging those issues, the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/diagnostics-pane-data-validity.html#bq8t6q3-36\">Array Bounds Exceeded<\/a> diagnostic can help.<\/p>\r\n\r\n<p><strong>Example<\/strong><\/p>\r\n\r\n<p>Let's say I have a simple model with a C-Mex S-Function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/example_times_two_model.png\" alt=\"Simple model using an s-function\" \/><\/p>\r\n\r\n<p>For some reasons, a bug got introduced in my S-Function: a pointer is incremented beyond its range in memory. In this case, the output signal is a scalar, but my S-function writes to it as if it was a vector with two elements:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/wrong_times_two.png\" alt=\"S-Function Code with a bug\" \/><\/p>\r\n\r\n<p>In a large model this could overwrite the output of another block, causing silent wrong results.<\/p>\r\n\r\n<p><strong>Debugging<\/strong><\/p>\r\n\r\n<p>One of the first thing I do when I receive models with inexplicable behavior, I look for s-functions using <tt>find_system<\/tt>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/find_system.png\" alt=\"Searching for S-function\" \/><\/p>\r\n\r\n<p>If there is an S-Function, I go to the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/diagnostics-pane-data-validity.html\">Data Validity Diagnostics<\/a> section of the model configuration:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/dataValidity.png\" alt=\"Data Validity Diagnostics\" \/><\/p>\r\n\r\n<p>and I set the Array Bounds Exceeded diagnostic to <strong>Error<\/strong>.<\/p> \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/ArrayBoundsDiagnostic.png\" alt=\"Array Bounds Exceeded Diagnostics\" \/><\/p>\r\n\r\n<p>When simulating the model, I immediately get an error explaining that the output port signal pointer has been incremented outside allotted limits.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/array_error.png\" alt=\"Array Bounds Exceeded Error Message\" \/><\/p>\r\n\r\n<p>This gives me a very good idea of where to focus when I will <a href=\"https:\/\/blogs.mathworks.com\/seth\/2012\/12\/06\/debugging-cc-s-functions\/\">attach a debugger and try to debug the s-function<\/a>.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>This diagnostic cannot catch 100% of the bugs in your s-functions, but it can help identifying a very common type of bug. I recommend using it carefully because it will add the overhead of checking array bounds after every call to your S-function, and slowdown your model.<\/p>\r\n\r\n<p>Do you use the Array Bounds Exceeded diagnostic when creating S-Function? Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=3080&#comment\">comment here<\/a>.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/array_error.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Ahhh... C Mex S-function... \r\n\r\nIf you ever tried creating a C Mex S-function and were a little rusty on your C programming, you might have experienced strange behavior or event MATLAB crashes. This... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/12\/19\/problems-with-your-s-functions-try-this-diagnostic\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,87],"tags":[354,355],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3080"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=3080"}],"version-history":[{"count":28,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3080\/revisions"}],"predecessor-version":[{"id":3142,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3080\/revisions\/3142"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=3080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=3080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=3080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}