Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Boost Exceptions cause a memory violation
QtWS25 Last Chance

Boost Exceptions cause a memory violation

Scheduled Pinned Locked Moved 3rd Party Software
11 Posts 3 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8 Offline
    8 Offline
    8bit
    wrote on last edited by
    #1

    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?

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

    try {
    interface.open(device);
    } 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 ***@

    Environment:
    @
    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)
    @

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on last edited by
      #2

      Do you known where is this pointer?
      Maybe you are killed by another exception? Try this:
      @
      try {
      interface.open(device);
      } 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.

      1 Reply Last reply
      0
      • 8 Offline
        8 Offline
        8bit
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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?

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • 8 Offline
            8 Offline
            8bit
            wrote on last edited by
            #5

            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?

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • 8 Offline
                8 Offline
                8bit
                wrote on last edited by
                #7

                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?

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

                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 {
                    interface.open(device);
                } catch (boost::system::system_error &error) {
                    std::cout << "We have a problem!" << std::endl;
                }
                

                }@

                1 Reply Last reply
                0
                • 8 Offline
                  8 Offline
                  8bit
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    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?

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • 8 Offline
                      8 Offline
                      8bit
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        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.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved