Protobuf crashes Mex-file

27 views (last 30 days)
Torsten Knüppel
Torsten Knüppel on 2 Mar 2019
Edited: Martijn on 23 Dec 2019
Dear all,
I would like to write a Mex-function that uses protobuf. Unfortunately, my program crashes after calling clear all or recompiling.
In order to illustrate my problem I wrote a reduced version. It consists of three files:
  1. An empty MEX-file called protoTest.cpp
#include "mex.hpp"
#include "mexAdapter.hpp"
class MexFunction : public matlab::mex::Function {
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
}
};
2. A very simple protobuf file called data3d.proto
syntax = "proto2";
package testproto;
message Data3d {
required float x = 1;
required float y = 2;
required float z = 3;
}
3. A Matlab script for compiling and running the whole thing
% Run protobuf compiler
system('protoc --cpp_out=./ ./*.proto');
% Compile mex file
mex('protoTest.cpp', 'data3d.pb.cc', '-lprotobuf');
% Run mex function
disp('Run..');
protoTest();
disp('ok!');
% Clear all
clear all
% Run mex function again -> Crash
disp('Run again..');
protoTest();
disp('ok!');
I have found similar error descriptions online, but in most cases memory leaks were the cause. I don't see this here.
Some things I have tried so far:
  • link the static version of libprotobuf -> didn't manage to get it working
  • compile the protobuf file into an object file first and afterwards link it to the mex-file -> no difference
  • play around with compiler/linker settings - no success, but I have too little experience with that and my attempts were mostly try-and-error based.
Any suggestions on how to make this work would be greatly appreciated.
Thanks in advance,
Torsten
  3 Comments
Sebastian Baur
Sebastian Baur on 12 Apr 2019
Edited: Sebastian Baur on 12 Apr 2019
I just saw a workaround to the clear all / clear mex problem. Using
mexLock();
in your mex file will make it unresponsive to clear commands. This way you can use Matlab normally after your first call to your mex function.
It reinforces the need for a restart of Matlab for recompiling though, as
mexUnlock();
will just bring you back to crashing after clear.

Sign in to comment.

Answers (1)

Martijn
Martijn on 23 Dec 2019
Edited: Martijn on 23 Dec 2019
It appears that this a more generic limitation of protocol buffers, see for example:
If that is indeed true and protocol buffers simply has not been designed to be loaded multiple times in a single process by "loading, unloading and then again loading" a dynamic library which was linked against protocol buffers, then I do not think it is feasible you can somehow fix/resolve this limitation in a MEX-file (which is in fact a dynamic library; which is loaded on the first call, unloaded by clear mex and then loaded again on the next call). What you might be able to do however is:
1. Prevent your MEX-file from being unloaded by the call to clear mex which is indeed accomplished by a mexLock() as mentiond in Sebastian's comment.
2. Try running your MEX-file in an external mexhost such that you can completely terminate and restart this mexhost process whenever you need to reload your MEX-file.

Community Treasure Hunt

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

Start Hunting!

Translated by