Force Matlab to execute .NET callback on main/single thread

8 views (last 30 days)
I wrote a simple matlab filewatcher object than uses .NET's FileSystemWatcher. It's a great little tool, works straight off the bat and you can add a Matlab callback directly. So far so good.
This worked perfectly in R2015a. I compile this in a larger stand-along application that my colleagues use and the .exe application works perfectly.
Enter R2015b (and R2016a): Works fine when running natively in Matlab but when run as a compiled .exe (even on my own machine) it crashes whenever the callback for the file watcher is asked to execute.
The reason for this is apparently that the callback tries to execute on a different thread and this crashes the application. This has been reported as bug 560506. It's been around for a long time (since R2009a) yet this doesn't happen in R2015a. In the bug report it says the workaround is to not invoke the callback MATLAB code delegate in a separate thread. Does anybody know how to do that?
I realise I could implement this in java but that requires java dev, it requires dragging a jar file along and requires "indirect" callback attachment. I also realize I could stay with R2015a but recently I have developed code with findgroups that obviously does not exist in R2015a.
So if anyone can point me in the right direction for forcing the callback execution on the main/single thread that would be absolutely great.
Regards, Phil

Answers (1)

Rahul Goel
Rahul Goel on 12 Apr 2016
Hi Philip
There is no full proof work around for this issue. You can minimize the chances of running into this issue however by making sure you do not create multiple objects like the System.IO.FileSystemWatcher object which would raise an .NET event on the same external event (i.e. a file changing).
To confirm that you are running into this exact issue, load the matlab_crash_dump.NNN.dmp file into Visual Studio, debug the managed part of the code and confirm that the actual Exception which occurred is:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Mike on 7 Jun 2016
I just tested 2016b prerelease and the issue appears to be resolved. I guess we will have to wait until the official release to confirm.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by