Search code examples
c++qtvisual-c++qlistqt5.1

QList declaration in header file causes a segmentation fault in the source file


I have encountered a very weird problem. It is similar to one described here: http://www.qtforum.org/article/20389/problems-with-qlist.html . When I am declaring a QList in my header file and I am trying to use it in the source file, the program fails with a segmentation fault.

Here is a snippet:

threads.h file:

class Corr2DThread 
{
    private: 
    QList<Sequence *> seqs_; 
    ...
}; 

threads.cpp file:

void Corr2DThread::addSequence(Sequence *seq)
{

     QLOGX("Thread " << idx_ << " adding new sequence (" << seqs_.size() << "), name: '" << seq->name() << "'"); //this code fails due to call seqs_.size() 
     QLOGINC;
     int activeCount = seq->activeItems();
     Q_ASSERT(activeCount > 0);
     QLOG("Contains " << activeCount << " active object images");
     seqs_.append(seq);

     QLOGDEC;
 }

However if I declare that QList locally, everythig is ok, as shown in the code below:

void Corr2DThread::addSequence(Sequence *seq)
{
     QList<Sequence *> seqs_; 

     QLOGX("Thread " << idx_ << " adding new sequence (" << seqs_.size() << "), name: '" << seq->name() << "'"); 
     QLOGINC;
     int activeCount = seq->activeItems();
     Q_ASSERT(activeCount > 0);
     QLOG("Contains " << activeCount << " active object images");
     seqs_.append(seq);

     QLOGDEC;
 }

I spent many hours trying to get this working, with no luck/knowledge. Any QT-guru can explain what is going on here?


Solution

  • Check if you forgot to actually create class instance. Next I kindly advise you to use GDB. 1) gdb 2) set follow-fork-mode child 3) run 4) You will obviously catch your segmentation fault - type "bt" to see the backtrace. 5) I highly presume you just operate on Corr2DThread pointer without having it initialized to constructed Corr2DThread.