justingardner / mgl

A suite of mex/m files for displaying psychophysics stimuli
http://justingardner.net/mgl
Other
18 stars 22 forks source link

eyeCalibDisp crashes MATLAB when "Display camera image" is selected #83

Closed hyunwoogu closed 1 year ago

hyunwoogu commented 1 year ago

System information

macOS 12.6, MATLAB R2022b, MGL(metal branch), Eyelink1000

Code to reproduce the issue

% initialize screen and eye-tracker
myscreen = initScreen();
myscreen = eyeCalibDisp(myscreen);

This prompts the following:

===========================
Enter: Display camera image
c: Start calibration routine
v: Start verification routine
a: autothreshold
ESC: Finished
For a full list of commands. Hit F1 on eye tracker
===========================

Anything else seems to work okay, but upon hitting Enter (=Display camera image) here, MATLAB crashes.


Attempts to track the issue

I tracked down what is causing this: eyeCalibDisp eventually calls mglPrivateEyelinkSetup.c in the following sequence:

eyeCalibDisp.m > calibrateEyeTracker.m > eyeCalibrationEyelink.m > mglEyelinkSetup.m > mglPrivateEyelinkSetup.c

It seems that do_tracker_setup() call within mglPrivateEyelinkSetup.c causes the above problem. To verify this, I put checkpoint print functions just above and below do_tracker_setup() as follows:

mexPrintf("Checkpoint1");
do_tracker_setup();
mexPrintf("Checkpoint2");

When I see the above prompt and complete calibration routines without hitting Enter, both the "Checkpoint1" and "Checkpoint2" are printed. On the other hand, when I see the above prompt and hit Enter, "Checkpoint1" is still printed but then MATLAB crashes without "Checkpoint2" being printed.

do_tracker_setup() seems to be EyeLink's function, not the MGL function. This function is located under /Library/Frameworks/eyelink_core.framework/Versions/A/Headers/core_expt.h (This Headers/ directory is included when we called mglMakeMetal() )

By the way, the only line that defines do_tracker_setup() within core_expt.h is the following: INT16 ELCALLTYPE do_tracker_setup(void);

Other than this, I cannot find the "contents" of this function do_tracker_setup. Also, not sure if it issue is about fixing this function or not.

justingardner commented 1 year ago

The issue was that the eye display code was not yet implemented for the metal version. I have written that code and checked it in and I have tested that it is working correctly.

However, there is an issue with using the Matlab Desktop with the code. If you run the code with Matlab from the terminal running

matlab -nodesktop

then everything is fine. It seems that there is something going on with the callbacks that Eyelink uses which doesn't play well with Matlab's desktop. It even crashes if you use Matlab -nodesktop and just run the editor. I suspect that it has something to do with threading or some process that gets started when Matlab runs the editor or desktop to track variables and the debugger.

Anyway, until we can get a fix for this issue, the Eyelink code should be run without desktop. It will warn you if you are using the desktop to run w/out the desktop.

justingardner commented 1 year ago

Just a further note for when we come back to this.

The code in mglPrivateEyelinkSetup.c now has a hook in it for when you press key F5. This simply calls the function mglFlush in matlab. This reliably crashes the system when run from the Desktop. Note that if you put the same call in the entrance Mex function (the keyboard call is a callback from the Eyelink code), the code does not crash. So it has something to do with the re-entrant callback. I've been checking this Matlab 2022b, maybe it works in earlier versions of Matlab (as there is nothing metal or new about all of this code). Probably worth checking an older version of matlab (from 2019 onwards) to see if this is a new issue. If it is a new issue then maybe it will get resolved by MathWorks.

justingardner commented 1 year ago

Closing this issue, as the above works as a fix.