0

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

3
  • Unless I'm missing something, there is no QQueue or QList in either of the repositories you linked. Commented Jun 1, 2012 at 8:23
  • Note: It looks like your "master" branch is not up to date, your relevant code is in "core". github.com/ilardm/lib-qtLogger/blob/core/inc/libqtlogger.h Commented Jun 1, 2012 at 8:35
  • @HostileFork current code is on branch core, but application repo uses correct revision. just to clarify Commented Jun 1, 2012 at 8:51

1 Answer 1

2

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! :-)

Sign up to request clarification or add additional context in comments.

4 Comments

Furthermore, this has nothing to do with cmake! I'm going to take those tags off and give this a more appropriate title (for people who might arrive here via Google search in the future...)
Oh shi.. You are correct. Thank you. P.S. LL_EROR is just a typo. I suppose I was too tired and sleepy while writing this code
+1 very nice catch! @IlyaArefiev, if you accept it you should also upvote it: stackoverflow.com/faq#howtoask
Glad I could help, and no worries, my Russian is terrible even when I'm wide awake. @Brady You'd think I'd get paid for this kind of thing, but...I like helpin' the kids. :) hostilefork.com/2012/01/18/stackoverflow-summaries-2011

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.