Note that there are some explanatory texts on larger screens.

plurals
  1. POQt QString cloning Segmentation Fault
    primarykey
    data
    text
    <p>I'm building my first Qt app using Qt Creator, and everything was going fine until I started getting a strange SIGSEGV from a line apparently harmless.</p> <p>This is the error:</p> <blockquote> <p>Program received signal SIGSEGV, Segmentation fault. 0x0804e2fe in QBasicAtomicInt::ref (this=0x0) at /usr/lib/qt/include/QtCore/qatomic_i386.h:120</p> </blockquote> <p>By backtracing the exception on <strong>gdb</strong>, I found that a simple getter is passing a NULL pointer to the clone constructor when I return my attribute.</p> <p>Backtrace output:</p> <blockquote> <p><code>(gdb) backtrace</code><br> <code>#0 0x0804e2fe in QBasicAtomicInt::ref (this=0x0) at /usr/lib/qt/include/QtCore/qatomic_i386.h:120</code><br> <code>#1 0x0804eb1b in QString (this=0xbfcc8e48, other=@0xbfcc8e80) at /usr/lib/qt/include/QtCore/qstring.h:712</code><br> <code>#2 0x0805715e in Disciplina::getId (this=0xbfcc8e7c) at disciplina.cpp:13</code><br> [...] </p> </blockquote> <p>Inspecting the pointer passed to the QString constructor:</p> <blockquote> <p>(gdb) x 0xbfcc8e80<br> 0xbfcc8e80: 0x00000000 </p> </blockquote> <p>And this is disciplina.cpp:13</p> <pre><code>QString Disciplina::getId() { return id; } </code></pre> <p>So, all points towards the copy constructor of QString receiving an empty pointer, which makes no sense to me. <strong>id</strong> was declared as a private QString.</p> <pre><code>private: QString id; </code></pre> <p>Well, I have no clue of what could be going on, and my debugging skills only go so far, so if anyone could throw an idea I'd be really glad.</p> <p>Thanks.</p> <p><strong>edit</strong></p> <p>More code, as requested.</p> <p><em>disciplina.h</em></p> <pre><code>#ifndef DISCIPLINA_H #define DISCIPLINA_H #include &lt;QString&gt; #include &lt;QMap&gt; #include "curso.h" #include "turma.h" class Curso; class Turma; class Disciplina { private: unsigned short int serie; QString id; QString nome; Curso* curso; QMap&lt;unsigned int, Turma*&gt; turmas; public: Disciplina(QString id, Curso* curso, QString nome, unsigned short int serie); QString getId(); const Curso getCurso(); QString getNome(); void setNome(QString nome); void addTurma(Turma* t, unsigned int id); QMap&lt;unsigned int, Turma*&gt; getTurmas(); }; #endif // DISCIPLINA_H </code></pre> <p><em>disciplina.cpp</em></p> <pre><code>#include "disciplina.h" Disciplina::Disciplina(QString id, Curso* curso, QString nome, unsigned short int serie) { this-&gt;id = id; this-&gt;curso = curso; this-&gt;nome = nome; this-&gt;serie = serie; } QString Disciplina::getId() { return id; } const Curso Disciplina::getCurso() { const Curso c(*this-&gt;curso); return c; } QString Disciplina::getNome() { return this-&gt;nome; } void Disciplina::setNome(QString nome) { this-&gt;nome = nome; } void Disciplina::addTurma(Turma* t, unsigned int id) { this-&gt;turmas.insert(id, t); } QMap&lt;unsigned int, Turma*&gt; Disciplina::getTurmas() { return this-&gt;turmas; } </code></pre> <p><em>Caller function (I broke it down for easier debugging)</em></p> <pre><code>Disciplina* MainWindow::getSelectedDisciplina() { if(ui-&gt;disciplinaTurma-&gt;count() &gt; 0 &amp;&amp; currentCurso) { QMap&lt;QString, Disciplina*&gt; qm(currentCurso-&gt;getDisciplinas()); QString key = ui-&gt;disciplinaTurma-&gt;itemText(ui-&gt;disciplinaTurma-&gt;currentIndex()); Disciplina* d = qm[key]; QMessageBox::information(this, d-&gt;getId(), d-&gt;getNome()); return d; } else return NULL; } </code></pre> <h2>Solved</h2> <p>The <code>Disciplina</code> object inserted into the map was getting out of scope and therefore deleted. Since, as Jacinto pointed out, Map created a vanilla value when you try to access a nonexistent key, it looked like the object was there.</p> <p>Thank you both <strong>Jacinto</strong> and <strong>sth</strong> for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload