Frage

I'm trying to create shared library (with CMake) which uses Qt objects (in particular QString and QQueue) inside and using this library with other application. The problem is that application recieves SIGSEGV when it calls methods of QQueue:

(gdb) r
Starting program: /home/ilya/projects/qtLogger/build/qtLoggerSample
[Thread debugging using libthread_db enabled]
startup
_DEBUG
QtLogger::QtLogger()
void QtLogger::foo(void*)
void QtLogger::log(QtLogger::LOG_LEVEL, QString) lvl: DEBUGmsg: "debug 1"

Program received signal SIGSEGV, Segmentation fault.
0x08049450 in QString (this=0x2817ad9c, other=...) at /usr/include/qt4/QtCore/qstring.h:714
714     inline QString::QString(const QString &other) : d(other.d)
(gdb) bt
#0  0x08049450 in QString (this=0x2817ad9c, other=...) at /usr/include/qt4/QtCore/qstring.h:714
#1  0xb7fdda0c in QList<QString>::node_construct (this=0x804b474, n=0x2817ad9c, t=...) at /usr/include/qt4/QtCore/qlist.h:352
#2  0xb7fdd7fa in QList<QString>::append (this=0x804b474, t=...) at /usr/include/qt4/QtCore/qlist.h:481
#3  0xb7fdd5e0 in QQueue<QString>::enqueue (this=0x804b474, t=...) at /usr/include/qt4/QtCore/qqueue.h:59
#4  0xb7fdd19f in QtLogger::log (this=0x804b460, level=QtLogger::LL_DEBUG, message=...)
    at /home/ilya/projects/qtLogger/lib-qtLogger/src/libqtlogger.cpp:97
#5  0x08049099 in main (argc=1, argv=0xbffff644) at    /home/ilya/projects/qtLogger/src/main.cpp:27

Source code of application can be found here: https://github.com/ilardm/qtLoggerSample/tree/137adee556f41eb4526e1d1c604e8541ef6eb65a

Source code of library can be found here(also available as git submodule of application repository): https://github.com/ilardm/lib-qtLogger/tree/bf1b490fd7c6666176c23e6fd791c00937d954b4

Could you please help me to understand where I'am wrong?

P.S. I'm using Qt 4.6.3, Debian Squeeze x86 and x64, gcc 4.4.5

War es hilfreich?

Lösung

You're corrupting your QQueue with your initialization of the string array. For those who (like me) are having trouble navigating your Sources/Branches/SubRepositories...your declaration of QtLogger looks like this:

class LIBQTLOGGER_EXPORT QtLogger
{
public:
    typedef enum {
        LL_EROR,
        LL_WARNING,
        LL_LOG,
        LL_DEBUG,

        LL_COUNT
    } LOG_LEVEL;

public:
    QtLogger();
    ~QtLogger();

public:
    void foo( void* );

    void log( LOG_LEVEL, QString );

protected:
    LOG_LEVEL currentLevel;
    QString ll_string[ LL_COUNT ];

    QQueue< QString > messageQueue;
    QMutex mqMutex;
};

And then your constructor looks like this:

QtLogger::QtLogger()
    : currentLevel( LL_DEBUG )
{
#if ENABLE_LOGGER_LOGGING
    std::clog << __PRETTY_FUNCTION__ << std::endl;
#endif

    ll_string[ LL_EROR      ].sprintf( "ERROR" );
    ll_string[ LL_WARNING   ].sprintf( "WARN " );
    ll_string[ LL_LOG       ].sprintf( "LOG  " );
    ll_string[ LL_DEBUG     ].sprintf( "DEBUG" );

    ll_string[ LL_COUNT     ].sprintf( "     " );
}

LL_EROR is 0, LL_COUNT is 4. (Sidenote: That word is spelled "ERROR", so your string is the correct bit.) Anyway...your declaration is effectively QString ll_string[4]; into which you are putting 5 strings and corrupting the subsequent member variable.

Why you put that empty string in there in the first place is a bit of a mystery to me... (!) But either way, vector classes are pretty light weight in the scheme of things rather than raw arrays, and often provide more checking on bounds for common operations. Use QVector (or whatever) in the future and it will be easier to catch this class of bug:

http://qt-project.org/doc/qt-4.8/qvector.html

In short, this has nothing to do with . Next time, try building everything into a single executable before assuming that's relevant! :-)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top