E N D
On Linux, there can be issues with calling a mex function that has been compiled with a different version of GCC than Mathworks used to compile Matlab. On some versions of Matlab, we have had significant problems with mex functions crashing Matlab due to library incompatibility problems. This is especially true when the mex functions link to large 3rd party libraries such as ffmpeg. The “real” solution is to have the user upgrade or downgrade GCC to a version that is wholly compatible with the one Mathworks used. Unfortunately, this is often infeasible since it essentially implies rebuilding the entire operating system. An alternative workaround is to compile a small mex function that makes minimal usage of the standard C library and thus it minimizes the chances of running into library compatibility problems. This minimal mex function then spawns a new process that implements the desired functionality. The mex function passes function arguments to the server process and reads results back via some interprocess communication protocol. This allows the server process to freely use any 3rd party library without needing to worry about compatibility problems with Matlab binaries. The approach we have used is to create a set of abstractions that mimic Matlab’s mex interface and matrix object. On systems without library problems, we translate the original mex call to our abstraction. On systems with library issues, we can serialize data over a set of operating system pipes to a server process. Our abstractions do not require linking to any binaries from Mathworks. The two methods are sketched below. A side benefit to using a server process is that when there are memory bugs in the backend code, only the server process is crashed, not all of Matlab. Depending on the developer’s coding and debugging preferences, this can lead to faster and more convenient debugging. If this architecture turns out to be useful for you or looks interesting, drop me a line at dalleyg@mit.edu. I can write up a fuller explanation and/or refine the architecture. Matlab Process videoReader_PLUGINPopen2Server Process mexFunction(…) videoReader_PLUGINPopen2.mex* mexClientPopen2.cpp main(…) videoReader_PLUGINPopen2 mexServerStdio.cpp handleMexRequest(…) … videoReaderWrapper.cpp videoReader.m PLUGINIVideo::… Matlab Process mexFunction(…) videoReader_PLUGINDirect.mex* mexClientDirect.cpp handleMexRequest(…) … videoReaderWrapper.cpp PLUGINIVideo::… videoReader.m