How to use mfc in de dll using def file - c++

I'm writing a plugin for a musicplayer named MusicBee.
I got one problem writing it. I must use mfc for a lot of thinks. Such as hbitmap, special timers and other stuff. But every time I change "Use of MFC" in VS2012 from "Use standard Windows library" to "use mfc ..." my code doenst compile. The problem is a def file that I must use for the plugin of MusicBee.
The def file looks like:
LIBRARY "mb_LogitechPlugin"
EXPORTS
Initialise #1
Configure #2
Close #3
Uninstall #4
ReceiveNotification #5
RetrieveLyrics #6
RetrieveArtwork #7
SaveSettings #8
Refresh #9
IsReady #10
GetIcon #11
FolderExists #12
GetFolders #13
GetFiles #14
FileExists #15
GetFile #16
GetFileArtwork #17
GetPlaylists #18
GetPlaylistFiles #19
GetStream #20
GetError #21
All other code can be found at:
https://bitbucket.org/jimmyD/musicbee-logitech-applet/src

You do not have to use MFC at all.
MFC (Microsoft Foundation Classes) are nothing more than a (bad) wrapper around the Win32 API. you do not need to use it all. If there are special things that MFC is doing for you that you cannot figure out, I suggest that you look at the included MFC source. It really is not worth it, just to get at HBITMAP or some other things.

Related

Crash when running QT application on Ubuntu Server

I am seeing that if I run a QT application on Ubuntu desktop edition I am able to run the application. If I take the same application and try and run it on Ubuntu server edition I am seeing a crash when starting the QT application. So far I have seen that I need to set QT to render offscreen with setting this environmental variable:
export QT_QPA_PLATFORM=offscreen
And then when I run the application I get this stack trace with the application crashing:
Thread 3 "hmi" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff7fa8700 (LWP 18084)]
0x00007fffdf88decb in ?? () from /Qt5.6.2/5.6/gcc_64/plugins/platforms/libqoffscreen.so
(gdb) bt
#0 0x00007fffdf88decb in ?? () from /Qt5.6.2/5.6/gcc_64/plugins/platforms/libqoffscreen.so
#1 0x00007fffdf88e283 in ?? () from /Qt5.6.2/5.6/gcc_64/plugins/platforms/libqoffscreen.so
#2 0x00007ffff399a78d in QOpenGLContext::create() () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Gui.so.5
#3 0x00007ffff41d2a67 in ?? () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Quick.so.5
#4 0x00007ffff41d32d2 in ?? () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Quick.so.5
#5 0x00007ffff39633ea in QWindow::event(QEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Gui.so.5
#6 0x00007ffff4206553 in QQuickWindow::event(QEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Quick.so.5
#7 0x00007ffff4eb25ca in QCoreApplication::notify(QObject*, QEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Core.so.5
#8 0x00007ffff4eb2720 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Core.so.5
#9 0x00007ffff3958c69 in QGuiApplicationPrivate::processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Gui.so.5
#10 0x00007ffff39597fd in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Gui.so.5
#11 0x00007ffff393aad3 in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Gui.so.5
#12 0x00007fffdf88e3f0 in ?? () from /Qt5.6.2/5.6/gcc_64/plugins/platforms/libqoffscreen.so
#13 0x00007fffe9eb3197 in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#14 0x00007fffe9eb33f0 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#15 0x00007fffe9eb349c in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#16 0x00007ffff4f01f07 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Core.so.5
#17 0x00007ffff4eb076a in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Core.so.5
#18 0x00007ffff4eb85fd in QCoreApplication::exec() () from /Qt5.6.2/5.6/gcc_64/lib/libQt5Core.so.5
I take the same application and run with offscreen set and on the desktop edition I am not seeing the same crash. I do not see much info about the QT library libqoffscreen.so and cannot find symbols for the prebuilt libraries to get a better stack trace. Is there anything that I may need to install on the Ubuntu server to allow me to run this QT application?
You’re trying to use Qt Quick, and this won’t work with the offscreen platform, as there’s no OpenGL support there. I don’t understand why you’d want to run such an application headless – it won’t be useful, presumably. Since it your code, you shouldn’t be showing the GUI in the headless mode: either add a command option to start without the GUI, or disable the GUI code using compile options (e.g. a macro passed from the makefile).

Protobuf version conflicts with Qt

I'm trying to use protobufs v 3.3.2 with Qt 5.9.1. This works with some Qt applications, but only if they are command line programs. Once I create a GUI application with Qt and protobufs, I get this error:
[libprotobuf FATAL
/home/mkraus/Documents/dev/star385/build/linux-desktop-debug-libs/protobuf/src/src/google/protobuf/stubs/common.cc:78]
This program was compiled against version 2.6.1 of the Protocol Buffer runtime library, which is not compatible with the installed
version (3.3.2). Contact the program author for an update. If you
compiled the program yourself, make sure that your headers are from
the same version of Protocol Buffers as your link-time library.
(Version verification failed in
"/build/mir-ui6vjS/mir-0.26.3+16.04.20170605/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)
I should clarify that my part of the code is certainly using version 3.3.2 (I'm downloading and compiling protobufs from the git sources and statically linking). Look at the stack trace below to see that something that Qt is referencing is causing a protobuf version mismatch.
I'm developing on Ubuntu 16.04 and using the default desktop environment (Unity).
Work-Arounds
My troubleshooting has revealed these symptoms and work-arounds:
Use KDE / KUbuntu. Changing the desktop environment when logging in completely avoids the version mismatch issue.
Run the Qt application with -platform eglfs. This runs the application in full-screen mode using OpenGL. The program runs, but the window size is incorrect. When using the -platform eglfs option, it works even in Unity, but without this option, it gives me the above error.
Any Qt application that is a command-line only application (using QCoreApplication instead of QGuiApplication) can use protobufs 3.3.2. Changing the same app to use a GUI causes the version mismatch issue.
Questions
How can I use protobufs 3.3.2 with Qt GUI applications, and also not be dependent on what desktop environment is in use? Is it Qt that is using the version 2.6.1 of protobufs, and if so, is it feasible to compile Qt to use protobufs 3.3.2?
Debug Info
Here is a stack trace (the program crashes almost immediately upon starting):
terminate called after throwing an instance of 'google::protobuf::FatalException'
what(): This program was compiled against version 2.6.1 of the Protocol Buffer runtime library, which is not compatible with the installed version (3.3.2). Contact the program author for an update. If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library. (Version verification failed in "/build/mir-ui6vjS/mir-0.26.3+16.04.20170605/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)
Thread 1 "scan" received signal SIGABRT, Aborted.
0x00007ffff4dff428 in __GI_raise (sig=sig#entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ffff4dff428 in __GI_raise (sig=sig#entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007ffff4e0102a in __GI_abort () at abort.c:89
#2 0x00007ffff543984d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff54376b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff5437701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff5437919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000000000603e0a in google::protobuf::internal::LogMessage::Finish (this=0x7fffffffc250)
at /home/mkraus/Documents/dev/star385/build/linux-desktop-debug-libs/protobuf/src/src/google/protobuf/stubs/common.cc:268
#7 0x0000000000603e5a in google::protobuf::internal::LogFinisher::operator= (this=0x7fffffffc20f, other=...)
at /home/mkraus/Documents/dev/star385/build/linux-desktop-debug-libs/protobuf/src/src/google/protobuf/stubs/common.cc:276
#8 0x0000000000603171 in google::protobuf::internal::VerifyVersion (headerVersion=2006001, minLibraryVersion=2006000,
filename=0x7fffde80aec0 "/build/mir-ui6vjS/mir-0.26.3+16.04.20170605/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc")
at /home/mkraus/Documents/dev/star385/build/linux-desktop-debug-libs/protobuf/src/src/google/protobuf/stubs/common.cc:86
#9 0x00007fffde7d490b in mir::protobuf::protobuf_AddDesc_mir_5fprotobuf_2eproto() ()
from /usr/lib/x86_64-linux-gnu/libmirprotobuf.so.3
#10 0x00007fffde7d2409 in ?? () from /usr/lib/x86_64-linux-gnu/libmirprotobuf.so.3
#11 0x00007ffff7de76ba in call_init (l=<optimized out>, argc=argc#entry=1, argv=argv#entry=0x7fffffffd5d8,
env=env#entry=0x7fffffffd5e8) at dl-init.c:72
#12 0x00007ffff7de77cb in call_init (env=0x7fffffffd5e8, argv=0x7fffffffd5d8, argc=1, l=<optimized out>) at dl-init.c:30
#13 _dl_init (main_map=main_map#entry=0xa2f450, argc=1, argv=0x7fffffffd5d8, env=0x7fffffffd5e8) at dl-init.c:120
#14 0x00007ffff7dec8e2 in dl_open_worker (a=a#entry=0x7fffffffc6e0) at dl-open.c:575
#15 0x00007ffff7de7564 in _dl_catch_error (objname=objname#entry=0x7fffffffc6d0, errstring=errstring#entry=0x7fffffffc6d8,
mallocedp=mallocedp#entry=0x7fffffffc6cf, operate=operate#entry=0x7ffff7dec4d0 <dl_open_worker>, args=args#entry=0x7fffffffc6e0)
at dl-error.c:187
#16 0x00007ffff7debda9 in _dl_open (file=0xa2f048 "/opt/Qt5.8.0/5.8/gcc_64/plugins/platformthemes/libqgtk3.so", mode=-2147479551,
caller_dlopen=0x7ffff599b7a8, nsid=-2, argc=<optimized out>, argv=<optimized out>, env=0x7fffffffd5e8) at dl-open.c:660
#17 0x00007ffff1806f09 in dlopen_doit (a=a#entry=0x7fffffffc910) at dlopen.c:66
#18 0x00007ffff7de7564 in _dl_catch_error (objname=0xa02b80, errstring=0xa02b88, mallocedp=0xa02b78,
operate=0x7ffff1806eb0 <dlopen_doit>, args=0x7fffffffc910) at dl-error.c:187
#19 0x00007ffff1807571 in _dlerror_run (operate=operate#entry=0x7ffff1806eb0 <dlopen_doit>, args=args#entry=0x7fffffffc910)
at dlerror.c:163
#20 0x00007ffff1806fa1 in __dlopen (file=<optimized out>, mode=<optimized out>) at dlopen.c:87
#21 0x00007ffff599b7a8 in ?? () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Core.so.5
#22 0x00007ffff5994fd5 in ?? () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Core.so.5
#23 0x00007ffff598a647 in QFactoryLoader::instance(int) const () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Core.so.5
#24 0x00007ffff6b392f1 in ?? () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Gui.so.5
#25 0x00007ffff6b43538 in QGuiApplicationPrivate::createPlatformIntegration() () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Gui.so.5
#26 0x00007ffff6b43edd in QGuiApplicationPrivate::createEventDispatcher() () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Gui.so.5
#27 0x00007ffff59a57d6 in QCoreApplicationPrivate::init() () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Core.so.5
#28 0x00007ffff6b456ab in QGuiApplicationPrivate::init() () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Gui.so.5
#29 0x00007ffff6b46364 in QGuiApplication::QGuiApplication(int&, char**, int) () from /opt/Qt5.8.0/5.8/gcc_64/lib/libQt5Gui.so.5
#30 0x00000000005c55bd in main (argc=1, argv=0x7fffffffd5d8) at /home/mkraus/Documents/dev/star385/src/linux/ui/scan/main.cpp:35
You can find here a discussion about the same issue and they talk about an interesting workaround.
It seems that this error is caused by the library libqgtk3.so located in /opt/Qt/5.9/gcc_64/plugins/platformthemes. If you don't need it in your project you can rename/remove it to make the error go away.
If you are using CMake as a build system you also need to comment all the lines in the file /opt/Qt/5.9/gcc_64/lib/cmake/Qt5Gui/Qt5Gui_QGtk3ThemePlugin.cmake to avoid configure issues.
To add on, the real problem comes from the library libmir which depends on the the libprotobuf. You may run on this problem whenever you try to use recent tensorflow with libgtk3.0 because of this hard dependency. As libmir depends on the system libprotobuf which is normally behind the version in use by tensorflow (which downloads its own version from the repository).
The good news, this BUG on libgtk was reported and fixed however, to use the fixed version you have to move to libgtk3.0 3.22 (see BUG report).
If you are using Qt from the Ubuntu package repository, you can remove the offending library by uninstalling qt5-gtk-platformtheme. This will remove libqgtk3.so and the corresponding CMake file without having to resort to hacks that might have unintended consequences.
As Blabdouze said, this error is caused by the libqgtk3 plugin which is used to set the GUI style. libqgtk3 uses the libmir system library, which uses protobuf 2.6.1. This leads to conflicts when the application starts.
I found a workaround that allows you to avoid editing of Qt files:
You need to copy the "plugins" folder from ".../Qt/5.хх.хх/gcc_64/" to some other location (for example, next to the project build folder).
Then you must remove "platformthemes/libqgtk3.so" and "platformthemes/libqgtk3.so.debug" from the copied folder.
In main(), before creating a QApplication instance, call the static function "QApplication::setLibraryPaths("path/to/copied/plugins/folder")".
Finally, you must add variable "LD_LIBRARY_PATH" with the value ".../Qt/5.хх.хх/gcc_64/lib" (correct path will depend on your Qt version) in a project's "environment settings" in Qt Creator. You also may add a "QT_DEBUG_PLUGINS" variable with a value of "1". It will allow you to check which plugins are used by your project and remove unnecessary plugins from the release version.
In conclusion I would like to note that this error occurred when running the project in Ubuntu 16.04, but it disappeared when I switched to version 18.04. It seems that in version 18.04 app uses the default Qt style instead of the GTK style.

Traits/UI (chaco) plotting application breaks, when run in a 32-bit Anaconda environment, on 64-bit Ubuntu 16.04

I have a Traits/UI (chaco) plotting application, which runs fine in the root environment of my Anaconda 64-bit installation, on 64-bit Ubuntu 16.04. However, when I try to run it from a 32-bit environment, which I created for testing, it fails.
It appears to be defaulting to the "null" graphical back-end toolkit for some reason, giving me a stack trace, which ends thusly:
File
"/home/dbanas/anaconda2/envs/pybert_32/lib/python2.7/site-packages/enable/toolkit.py",
line 40, in _init_toolkit
format_exception_only(t, v)) ImportError: Unable to import the image backend for the null toolkit (reason: ['ImportError: libGL.so.1:
cannot open shared object file: No such file or directory\n']).
So, I realized that I didn't have 32-bit Gtk+ library support installed on my system. I installed that. And, now, the application segment faults when I launch it in my 32-bit environment! Here is the stack trace, from gdb:
> #0 0xf76e7f10 in ?? () from /lib/ld-linux.so.2
> #1 0xf76e8a4b in ?? () from /lib/ld-linux.so.2
> #2 0xf76ea7c9 in ?? () from /lib/ld-linux.so.2
> #3 0xf76f26bf in ?? () from /lib/ld-linux.so.2
> #4 0xf76ee204 in ?? () from /lib/ld-linux.so.2
> #5 0xf76f1f02 in ?? () from /lib/ld-linux.so.2
> #6 0xf74febf5 in ?? () from /lib/i386-linux-gnu/libdl.so.2
> #7 0xf76ee204 in ?? () from /lib/ld-linux.so.2
> #8 0xf74ff30d in ?? () from /lib/i386-linux-gnu/libdl.so.2
> #9 0xf74fec9e in dlopen () from /lib/i386-linux-gnu/libdl.so.2
> #10 0xf76523e6 in _PyImport_GetDynLoadFunc (fqname=0x8b72188 "PyQt4.QtCore", shortname=0x8b7218e "QtCore",
> pathname=0x8b73190 "/home/dbanas/anaconda2/envs/pybert_32/lib/python2.7/site-packages/PyQt4/QtCore.so",
>
> fp=0x8981b98) at Python/dynload_shlib.c:130
> #11 0xf7636512 in _PyImport_LoadDynamicModule (name=0x8b72188 "PyQt4.QtCore",
> pathname=0x8b73190 "/home/dbanas/anaconda2/envs/pybert_32/lib/python2.7/site-packages/PyQt4/QtCore.so",
>
> fp=0x8981b98) at ./Python/importdl.c:42
> #12 0xf76343c0 in load_module (name=0x8b72188 "PyQt4.QtCore", fp=<optimized out>,
> pathname=0x8b73190 "/home/dbanas/anaconda2/envs/pybert_32/lib/python2.7/site-packages/PyQt4/QtCore.so",
> type=3,
> loader=0x0) at Python/import.c:1937
> #13 0xf7634c43 in import_submodule (mod=0xf2b9c26c, subname=0x8b7218e "QtCore", fullname=0x8b72188 "PyQt4.QtCore")
> at Python/import.c:2725
> #14 0xf7634ebc in load_next (mod=0xf2b9c26c, altmod=0xf2b9c26c, p_name=0xff97d7fc, buf=0x8b72188 "PyQt4.QtCore",
> p_buflen=0xff97d7f8) at Python/import.c:2539
> #15 0xf76354fb in import_module_level (level=-1, fromlist=0xf2b33c8c, locals=<optimized out>, globals=0xf2b2de84,
> name=0x0) at Python/import.c:2256
> #16 PyImport_ImportModuleLevel (name=0xf2b35264 "PyQt4.QtCore", globals=0xf2b2de84, locals=0xf2b2de84,
> fromlist=0xf2b33c8c, level=-1) at Python/import.c:2312
> #17 0xf7615e76 in builtin___import__ (self=0x0, args=0xf2b352fc, kwds=0x0) at Python/bltinmodule.c:49
> #18 0xf75b6188 in PyCFunction_Call (func=0xf72cf78c, arg=0xf2b352fc, kw=0x0) at Objects/methodobject.c:85
> #19 0xf756bb9c in PyObject_Call (func=0xf72cf78c, arg=0xf2b352fc, kw=0x0) at Objects/abstract.c:2547
> #20 0xf7616454 in PyEval_CallObjectWithKeywords (func=0xf72cf78c, arg=0xf2b352fc, kw=0x0) at Python/ceval.c:4221
> #21 0xf761a8aa in PyEval_EvalFrameEx (f=0xf4ba19cc, throwflag=0) at Python/ceval.c:2624
> #22 0xf761ff25 in PyEval_EvalCodeEx (co=0xf2b361d0, globals=0xf2b2de84, locals=0xf2b2de84, args=0x0, argcount=0,
> kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:3584
> ---Type <return> to continue, or q <return> to quit---
So, I just made some progress:
Making the following code change, in my Python application:
from traits.trait_base import ETSConfig
# ETSConfig.toolkit = 'qt4'
ETSConfig.toolkit = 'wx'
resulted in some helpful error messages being printed out to my console, regarding missing libraries. After several iterations of the sequence:
dpkg -S libGLU.so.1
sudo apt-get install libglu1-mesa:i386
my application came up! However, the colors of the GUI are all screwed up, and when I first launch it, I get a warning dialog with the following message:
iCCP: known incorrect sRGB profile 07:07:37
So, now, I have two questions:
How do I fix the colors of the "wx" version?
How do I get the "qt4" version to spit out the same sort of helpful messages about missing libraries, instead of just seg. faulting?
Aha! Installing those missing 32-bit libraries has fixed the seg. faulting, and my "qt4" version is running, as well. And, the GUI colors are correct! :)
The problem was missing 32-bit libraries, which is not surprising, since I'm running 64-bit Ubuntu. However, getting the system to tell me which libraries were missing required changing the Python GUI application from the qt4 back-end to the wx back-end, like so:
from traits.trait_base import ETSConfig
# ETSConfig.toolkit = 'qt4'
ETSConfig.toolkit = 'wx'
The 32-bit library installations, which I was prompted to perform by trying to get the "wx" mode running, were enough to get the "qt4" mode running, as well. And, that's important, because the "wx" mode produces goofy GUI colors, while the "qt4" mode produces correct GUI colors.

C++ Mex file crashes matlab on 64bit linux, but not 32 bit windows, but program runs fine outside Matlab

WARNING The code I provide in my question may crash matlab AND your machine!!!
I have written a mex gateway function to a C++ class. If I compile this mex function on 32Bit windows using R2008a I have no problems. If I compile and run on Matlab R2011a running 64bit Scientific Linux (a version of Red Hat Enterprize Linux) matlab exits with a segfault when the mexfunction is called, although it appears to run about halfway through the program. The C++ class can be compiled and run (with a main function) outside of Matlab on both platforms with no errors. I am using Microsoft Visual C++ Express Edition on the windows machine, and gcc 4.4.5 on the Linux machine.
What is the cause of this and can I fix it?
A zip file containing the code and data files necessary to reproduce the problem can be downloaded from http://www.see.ed.ac.uk/~s0237326/downloads/mexcrash.zip. This zip file contains the .m and .cpp source code, and a text file for testing (Temp.fem, 10kB). The file fmeshersetup.m shows the commands I am using to compile. The file Test_mexfmesher.m runs the mexfunction with an appropriate input for testing. The mex gateway function is mexfmesher.cpp, it calls the fmesher class which is made up of the files in the fmesher directory.
There are a total of 13 C++ source files, which is a bit much to expect someone to debug, even the particular line on which the segfault occurs woud be of interest.
Unfortunately I do not have access to a graphical debugger on 64bit linux that can interface with Matlab, and was hoping someone would immediately see the problem, or it would be a known problem. As stated, when run not as a mex file, the program runs fine, so I cannot locate the error in another tool, and I'm not very familiar with gdb. I believe the problem might be related to a calls to the C++ function 'free'.
Below is a backtrace from the segfault:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff721be36 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwservices.so
(gdb) backtrace
#0 0x00007ffff721be36 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwservices.so
#1 0x00000034daa914f2 in std::basic_ostream<char, std::char_traits<char> >::flush() () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libstdc++.so.6
#2 0x00007ffff72302b0 in ioFlush () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwservices.so
#3 0x00007ffff61303f5 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#4 0x00007ffff61330ec in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#5 0x00007ffff6130c7a in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#6 0x00007ffff6131741 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#7 0x00007ffff618a7d9 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#8 0x00007ffff686d7ef in Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**) () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_dispatcher.so
#9 0x00007ffff61731f0 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#10 0x00007ffff6114975 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#11 0x00007ffff612e96e in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#12 0x00007ffff61330ec in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#13 0x00007ffff6130c7a in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#14 0x00007ffff6131741 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#15 0x00007ffff618a7d9 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#16 0x00007ffff686d7ef in Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**) () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_dispatcher.so
#17 0x00007ffff61669b2 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#18 0x00007ffff6128e13 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#19 0x00007ffff6127eb7 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#20 0x00007ffff6128397 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwm_interpreter.so
#21 0x00007ffff6d378fe in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwbridge.so
#22 0x00007ffff6d384ae in mnParser () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwbridge.so
#23 0x00007ffff6ae0d39 in mcrInstance::mnParser_on_interpreter_thread() () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#24 0x00007ffff6ac3db2 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#25 0x00007ffff6ac3ec0 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#26 0x00007fffee8badb6 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwuix.so
#27 0x00007fffee8c413d in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwuix.so
#28 0x00007fffef3110bd in sysq::during_F<boost::weak_ptr<sysq::ws_ppeHook>, boost::shared_ptr<sysq::ws_ppeHook> > std::for_each<__gnu_cxx::__normal_iterator<boost::weak_ptr<sysq::ws_ppeHook>*, std::vector<boost::weak_ptr<sysq::ws_ppeHook>, std::allocator<boost::weak_ptr<sysq::ws_ppeHook> > > >, sysq::during_F<boost::weak_ptr<sysq::ws_ppeHook>, boost::shared_ptr<sysq::ws_ppeHook> > >(__gnu_cxx::__normal_iterator<boost::weak_ptr<sysq::ws_ppeHook>*, std::vector<boost::weak_ptr<sysq::ws_ppeHook>, std::allocator<boost::weak_ptr<sysq::ws_ppeHook> > > >, __gnu_cxx::__normal_iterator<boost::weak_ptr<sysq::ws_ppeHook>*, std::vector<boost::weak_ptr<sysq::ws_ppeHook>, std::allocator<boost::weak_ptr<sysq::ws_ppeHook> > > >, sysq::during_F<boost::weak_ptr<sysq::ws_ppeHook>, boost::shared_ptr<sysq::ws_ppeHook> >) ()
from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libuij.so
#29 0x00007fffef312989 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libuij.so
#30 0x00007fffef30f4ae in svWS_ProcessPendingEvents(int, int, bool) () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libuij.so
#31 0x00007ffff6ac21c7 in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#32 0x00007ffff6ac260a in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#33 0x00007ffff6ac2d6f in ?? () from /opt/matlab-2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so
#34 0x00000034d7a077e1 in start_thread () from /lib64/libpthread.so.0
#35 0x00000034d72e573d in clone () from /lib64/libc.so.6
(gdb) quit
You have attempted to use 'free' to free memory with a pointer which actually points to NULL. A careful programmer would check if this is the case before attempting to use free. Some libraries will have an automatic check for this.
The reason you did not notice this when compiling as a standalone may be due to the fact that matlab uses its own libstdc++ (stored in /opt/matlab-2011a/bin/glnxa64/libstdc++.so.6) instead of the main one (stored in /usr/lib/x86_64-linux-gnu/).
If you are sudoer, You should force matlab to use the main libstdc++, this can solve many problems with mex files:
cd /opt/matlab-2011a/bin/glnxa64/
sudo mkdir old
sudo mv libstdc++.so.6* old
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 libstdc++.so.6

How do I interpret X-program backtrace report?

I'm playing with X-development. I've got a basic proto-WM going that works for a while and then produces these errors after a fairly random time.
[edit]
Locking assertion failure. Backtrace:
#0 /usr/lib/libxcb-xlib.so.0 [0x7f71dcf9a9ac]
#1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_unlock+0x24) [0x7f71dcf9aa54]
#2 /usr/lib/libX11.so.6 [0x7f71ddefe340]
#3 /usr/lib/libX11.so.6(XAllocColor+0xc1) [0x7f71ddedada1]
#4 /home/mais/code/simplewin/bin/Debug/simplewin [0x408e0b]
#5 /home/mais/code/simplewin/bin/Debug/simplewin [0x409062]
#6 /home/mais/code/simplewin/bin/Debug/simplewin [0x407a9d]
#7 /home/mais/code/simplewin/bin/Debug/simplewin [0x406c6d]
#8 /home/mais/code/simplewin/bin/Debug/simplewin [0x402734]
#9 /home/mais/code/simplewin/bin/Debug/simplewin [0x407e37]
#10 /home/mais/code/simplewin/bin/Debug/simplewin [0x407304]
#11 /home/mais/code/simplewin/bin/Debug/simplewin [0x407335]
#12 /lib/libpthread.so.0 [0x7f71ddc9afc7]
#13 /lib/libc.so.6(clone+0x6d) [0x7f71dd26a59d]
Locking assertion failure. Backtrace:
#0 /usr/lib/libxcb-xlib.so.0 [0x7f71dcf9a9ac]
#1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_lock+0x17) [0x7f71dcf9ab17]
#2 /usr/lib/libX11.so.6 [0x7f71ddefe420]
#3 /usr/lib/libX11.so.6 [0x7f71ddefeb5b]
#4 /usr/lib/libX11.so.6 [0x7f71ddefeeb5]
#5 /usr/lib/libX11.so.6(XNextEvent+0x68) [0x7f71ddee5898]
#6 /home/mais/code/simplewin/bin/Debug/simplewin [0x404bc2]
#7 /lib/libpthread.so.0 [0x7f71ddc9afc7]
#8 /lib/libc.so.6(clone+0x6d) [0x7f71dd26a59d]
It looks like some kind of synchronization bug in my app; that much of the time the resources used are released in the correct sequence, but at some stage this get mixed-up and then the error(s) occur.
How do I interpret the above to find the location/cause of the errors?
1) build your program with debug symbold (on gcc, pass -g option)
2) enable core files, and once your application crashes, open it in gdb with core file, and check the backtrace
3) is your application multithreaded and using xlib? If so, you need to enable multi-threading access to the xlib functions.
4) install ddd, and run your application using that (ddd is a gui frontend for the linux debuger gdb)