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_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
rosbuild_add_executable(matlabLibServer src/matlabLibServer.cpp)
rosbuild_add_executable(matlabLibClient src/matlabLibClient.cpp)
#Link with vigenere and with the MCR
target_link_libraries (matlabLibServer libvigenere.so mwmclmcrrt)
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 ...
This is a memory corruption, can you update your question with a stack trace from GDB?
Thanks a lot for your help jbohren. I've updated my question now.