"Internal problem" when running a MEX file the second time

7 views (last 30 days)
Hi, I am having a problem when trying to run a .mexmaci64 file written by C++ with OpenCV the second time. I really appreciate if someone could help me.
Here is the test I did: I created a .dylib by C++ and some OpenCV stuff, and compiled it in the terminal with something like this:
g++ -dynamiclib Test.cpp -fvisibility=hidden -o libTest.dylib `pkg-config --cflags --libs opencv`
Then, in the mexFunction, I loaded this .dylib and did nothing else. It could run properly the first time, but Matlab popped up a window saying "Matlab encountered a internal problem... bla bla" when I ran it the second time.
I think it's because the dylib with OpenCV couldn't be loaded properly the second time, since I tried the same thing without the OpenCV stuff and everything is fine.
Does anyone know how to solve this? Thank you!

Answers (1)

Ken Atwell
Ken Atwell on 18 Jun 2012
Does the OpenCV library have a "close" function or something similar to call before the library is unloaded? Assuming there is, you could call this as the last operation in Test.cpp.
  1 Comment
Liu
Liu on 18 Jun 2012
Hi Ken,
Thanks for your answer. I used the dlopen() and dlclose() in a function TestArray() to load and close the dylib, and mexFunction will call TestArray(). It's something like this:
void TestArray() {
//....
void *hLib = dlopen(libName, RTLD_LOCAL);
if (!hLib) {
printf("Unable to open library:\n%s\n", libName);
printf("%s\n", dlerror());
return;
}
//......
if (dlclose(hLib)) {
printf("Unable to close library:\n%s\n", dlerror());
}
}
I tried to print something out to check at which step it crashes, and it turned out to be the dlopen() part when called the second time. I tried to put it in a try-catch, but MATLAB still reports the same thing. Maybe I should reorganise my code and separate the OpenCV part; it seems really confusing...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!