# MATLAB MRC Engine and ROS

Hi All

First of all, sorry, if this question has been asked already. I've indeed come across several questions that seem very related, but since the errors I'm getting are not the same I figured I'd just ask. This (question/57991/use-matlab-code-within-ros/) is very related but it doesn't completely ask the same question. This one (/question/33326/matlab-engine-and-ros-library-incompatible/) is also relevant, but the error reported is different from mine.

I'm using fuerte under Ubuntu 12.04 Precise and I would like to call some MATLAB functions that I have converted to MATLAB C++ Shared Libraries using MATLAB's mcc compiler (MATLAB R2012b in Ubuntu 12.04).

If I don't use ROS, but just a simple C++ example with CMake, everything works perfectly, and I'm able to call the MATLAB functions without problems.

However, when I try to do the same from a C++ ROS node, my node crashes with an strange error just after I try to execute the following line:

if (!mclInitializeApplication(NULL,0))


I get the error:

matlabLibServer: malloc.c:2451: sYSMALLOc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.


Aborted (core dumped)

The CMakeLists that I use is:

# Add MATLAB MCR include directories and lib directories
include_directories (${PROJECT_SOURCE_DIR}/lib /usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/extern/include/cpp /usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/extern/include) # Add library directories for the libvigenere library and the MCR libraries link_directories (${PROJECT_SOURCE_DIR}/lib /usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/runtime/glnxa64)

#uncomment if you have defined messages
rosbuild_genmsg()
#uncomment if you have defined services
rosbuild_gensrv()

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp) #target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_link_boost(${PROJECT_NAME} thread) #rosbuild_add_executable(example examples/example.cpp) #target_link_libraries(example${PROJECT_NAME})

#Link with vigenere and with the MCR


Note however, that compiling and linking work perfectly.

Is there anyway to link to MATLAB Shared C++ libraries while using ROS at the same time? What I would like is to implement a ROS service that basically calls a MATLAB function with some arguments and then I get the output back (a couple of double arrays). This service would be called rarely but the MATLAB routine takes quite a while to complete (1-3 sec). I would like to avoid using other third party projects such as rosbridge or IPC-MATLAB bridge if possible..

Finally, if I was able to use MATLAB coder to completely generate C code for my current MATLAB functions instead of converting them to MATLAB MCR Shared Libraries, would my problem be solved? Has anybody had success with that?

Thanks a lot for your help.

### UPDATE:

This gets interesting. So if I use the same source file, but I comment all the ROS parts (ros::init ...

edit retag close merge delete

This is a memory corruption, can you update your question with a stack trace from GDB?

( 2013-03-26 16:31:52 -0500 )edit

Thanks a lot for your help jbohren. I've updated my question now.

( 2013-03-27 05:10:30 -0500 )edit

Sort by » oldest newest most voted

Hi Javier

Unfortunately, I wasn't able to solve the problem and didn't even consider recompiling ROS. Because the way I was using my MATLAB code was pretty simple and I didn't have enough time to make things right, I ended up using the typical "hack" of making two programs: a standard C++ ROS node with no dependencies to the MATLAB code or MCR and another C++ program (not a ROS node) that linked to the MCR and the MATLAB shared library.

The C++ ROS node wrote the arguments needed for the MATLAB code as input files in a common folder and then called the C++ program that linked with the MCR. That same program then wrote the output to a common folder that was, at the end, read and parsed by the ROS node.

One final detail that I had to change is that the MCR installation was asking me to change my LD_LIBRARY_PATH, which was raising problems everywhere else in my system (OpenRAVE for example). Therefore, I wrote a simple bash script that just changed the LD_LIBRARY_PATH just before calling the program that linked to the MCR

echo Modifying LD_LIBRARY_PATH
old_lib_path=$LD_LIBRARY_PATH export LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64:$LD_LIBRARY_PATH
export XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/X11/app-defaults
echo LD_LIBRARY_PATH changed to $LD_LIBRARY_PATH bin/PFTExpertNode export LD_LIBRARY_PATH=$old_lib_path
`

As you can see, this is a really bad hack, but, because my application was simple enough, it was good enough for my needs.

more

Thanks! I managed by using Ubuntu 12.10 and Matlab 2013a, then there was no problem as the boost version is the same.

( 2013-05-20 03:49:26 -0500 )edit

Hi Enrique,

I'm having the same problem, and wondering if you manage to solve it (ideally without recompiling ROS).

Thanks! Javier

more