Protobuf crashes Mex-file

7 ビュー (過去 30 日間)
Torsten Knüppel
Torsten Knüppel 2019 年 3 月 2 日
編集済み: Martijn 2019 年 12 月 23 日
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 件のコメント
Torsten Knüppel
Torsten Knüppel 2019 年 4 月 8 日
Hi Nishanth,
unfortunately not.
There is something mentioned about problems with clashing symbols on the CMake documentation page for their FindMatlab package (https://cmake.org/cmake/help/v3.14/module/FindMatlab.html#known-issues) - but I'm not sure that this is relevant here. Adding the given linker options didn't solve the problem for me.
Best regards
Torsten
Sebastian Baur
Sebastian Baur 2019 年 4 月 12 日
編集済み: Sebastian Baur 2019 年 4 月 12 日
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.

サインインしてコメントする。

回答 (1 件)

Martijn
Martijn 2019 年 12 月 23 日
編集済み: Martijn 2019 年 12 月 23 日
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.

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by