简体   繁体   English

头文件中的QList声明导致源文件中的分段错误

[英]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 . 它类似于此处描述的内容: 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. 当我在头文件中声明QList并尝试在源文件中使用它时,该程序失败并出现分段错误。

Here is a snippet: 这是一个片段:

threads.h file: thread.h文件:

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

threads.cpp file: thread.cpp文件:

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: 但是,如果我在本地声明QList,那么一切都可以,如下面的代码所示:

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? 任何QT专家都可以解释这里发生了什么?

Check if you forgot to actually create class instance. 检查您是否忘记了实际创建类实例。 Next I kindly advise you to use GDB. 接下来,我建议您使用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. 1)gdb 2)设置跟随叉模式的孩子3)运行4)您显然会遇到分段错误-输入“ bt”以查看回溯。 5) I highly presume you just operate on Corr2DThread pointer without having it initialized to constructed Corr2DThread. 5)我高度假定您只对Corr2DThread指针进行操作,而没有将其初始化为构造的Corr2DThread。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM