Qt World Summit: Register Today!

Boost Exceptions cause a memory violation

  • Hi,

    I'm using boost::asio::serial_port to connect to a serial device in Qt. Everything compiles, links and works like charm. Except when an exception is thrown by the boost library. Somehow a pointer gets freed that was not allocated, only I can't trace the source of the breakdown using the debugger. I've included a simple example that raises an exception. It only happens in Qt applications, the provided example works fine when compiled in a normal c++ environment.

    Has anyone experienced something similar and/or have a possible solution for this problem?

    boost::asio::io_service io_service;
    boost::asio::serial_port interface(io_service);
    std::string device("some_non_existing_device");

    try {
    } catch (boost::system::system_error &error) {
    qDebug() << "We have a problem!";

    Application Output:
    @Brainwave(23504,0x7fff76ec6960) malloc: *** error for object 0x101398a40: pointer being freed was not allocated ***@

    Mac OS X Lion 10.7.2 (I know... it's not officially supported)
    Qt Creator 2.3.1
    Based on Qt 4.7.4 (64 bit)
    Boost 1.48.0 (also tried 1.47.0)

  • Do you known where is this pointer?
    Maybe you are killed by another exception? Try this:
    try {
    } catch (boost::system::system_error &error) {
    qDebug() << "We have a problem!";
    } catch ( ... ) {
    qDebug() << "Ups we are here!";
    If this not help then try a Valgrind.

  • The exception is clearly thrown by Boost, the generic catch didn't help.

    Stacktrace: The first 5 calls only show disassembler output :(
    @0 malloc_error_break
    1 free
    2 std::string::_Rep::_M_dispose
    3 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string
    4 std::runtime_error::~runtime_error
    5 boost::system::system_error::~system_error
    6 boost::asio::detail::do_throw_error
    7 boost::asio::detail::throw_error
    8 boost::asio::basic_serial_portboost::asio::serial_port_service::open
    9 MainWindow::MainWindow
    10 main

    I'll try to give Valgrind a go, never used that before. Thanks

  • I would suggest running the snippet in a debugger do some step by step debugging.

    For the time being, do you get any qDebug() output?
    Do you have any code after the try-catch block?

  • Hmm I can't find the origine of the problem with Valgrind either. However I have tested the code on my Ubuntu desktop and there the program seems to be running just fine. So i'll guess it's just the good old Mac problems as usual...

    Any thoughts on when the next official release will be available for OS X 10.7?

    Btw, when I run the program inside Valgrind that catches the faulty frees I do get the correct output from qDebug...

  • The Mac uses g++ as well, I doubt it's Mac specific.

    I suggest building your program in debug mode and run through it in Qt Creator. It shows you the exact source code lines, where the error occurs and you can double click and jump to the error.

  • I have narrowed down the origine of the problem by creating a single source file and compiling it on the command line by hand. (see files, below)

    During compilation qmake adds a "-mmacosx-version-min=10.5" flag to the compiler options causing the compiler to include some legacy stuff. And somehow causing the program to crash when an exception is thrown.

    When using qmake and make it crashes:
    @$ qmake
    $ make
    $ ./Boost
    Boost(59721,0x7fff76ec6960) malloc: *** error for object 0x100413ea0: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6@

    When the file is compiled and linked by hand and the -m flag is modified it works fine.
    @$ c++ main.cpp -mmacosx-version-min=10.7 -lboost_system -F/Users/roy/QtSDK/Desktop/Qt/474/gcc/lib -L/Users/roy/QtSDK/Desktop/Qt/474/gcc/lib -framework QtCore
    $ ./a.out
    We have a problem!

    I tried to change the flags that qmake specifies for the compiler. I've looked at the Build settings as well as the Toolchain settings but I can't find a solution. Can anyone explain me how to change them in QtCreator?

    QT += core
    QT -= gui
    LIBS = -lboost_system
    TARGET = Boost
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp

    @#include <iostream>
    #include <boost/asio.hpp>

    int main(void) {
    boost::asio::io_service io_service;
    boost::asio::serial_port interface(io_service);
    std::string device("some_non_existing_device");

    try {
    } catch (boost::system::system_error &error) {
        std::cout << "We have a problem!" << std::endl;


  • I ended up replacing all occurrences of 10.5 for 10.7 of the Mac OS X qmake mkspec in ~/QtSDK/Desktop/Qt/474/gcc/mkspecs/common/mac-g++.conf. This solves the problem for the time being.

    Should I report this as a bug/problem in the tracker?

    Thanks for all the suggestions.

  • It's hardly a Qt problem.
    You always must use all libs and binaries compiled against the same SDK version. Did you install precompiled boost and Qt libs or did you compile manually?

  • [quote author="Volker" date="1322236035"]Did you install precompiled boost and Qt libs or did you compile manually?[/quote]

    I've compiled boost locally and installed the QtSDK via the web installer so i guess the QtLibs are precompiled. Which indeed might explain the origine of the problem.

  • Yes, the Qt libs are precompiled. To use boost together with those you will have to compile it with the same settings. It will not help to change 10.5 to 10.7 in the mkspecs, as Qt is still built with the old settings!

    You have two options here:

    • build boost with the same settings as Qt
    • build Qt manually with the same settings as you build boost

    I personally would go with the first option and rebuild Qt.

Log in to reply