views:

510

answers:

3

Hi, 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.

This is the error:

Program received signal SIGSEGV, Segmentation fault. 0x0804e2fe in QBasicAtomicInt::ref (this=0x0) at /usr/lib/qt/include/QtCore/qatomic_i386.h:120

By backtracing the exception on gdb, I found that a simple getter is passing a NULL pointer to the clone constructor when I return my attribute.

Backtrace output:

(gdb) backtrace
#0 0x0804e2fe in QBasicAtomicInt::ref (this=0x0) at /usr/lib/qt/include/QtCore/qatomic_i386.h:120
#1 0x0804eb1b in QString (this=0xbfcc8e48, other=@0xbfcc8e80) at /usr/lib/qt/include/QtCore/qstring.h:712
#2 0x0805715e in Disciplina::getId (this=0xbfcc8e7c) at disciplina.cpp:13
[...]

Inspecting the pointer passed to the QString constructor:

(gdb) x 0xbfcc8e80
0xbfcc8e80: 0x00000000

And this is disciplina.cpp:13

QString Disciplina::getId()
{
    return id;
}

So, all points towards the copy constructor of QString receiving an empty pointer, which makes no sense to me. id was declared as a private QString.

private:
    QString id;

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.

Thanks.

edit

More code, as requested.

disciplina.h

#ifndef DISCIPLINA_H
#define DISCIPLINA_H
#include <QString>
#include <QMap>
#include "curso.h"
#include "turma.h"

class Curso;

class Turma;

class Disciplina
{
private:
    unsigned short int serie;
    QString id;
    QString nome;
    Curso* curso;
    QMap<unsigned int, Turma*> 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<unsigned int, Turma*> getTurmas();
};

#endif // DISCIPLINA_H

disciplina.cpp

#include "disciplina.h"

Disciplina::Disciplina(QString id, Curso* curso, QString nome, unsigned short int serie)
{
    this->id = id;
    this->curso = curso;
    this->nome = nome;
    this->serie = serie;
}

QString Disciplina::getId()
{
    return id;
}

const Curso Disciplina::getCurso()
{
    const Curso c(*this->curso);
    return c;
}

QString Disciplina::getNome()
{
    return this->nome;
}

void Disciplina::setNome(QString nome)
{
    this->nome = nome;
}

void Disciplina::addTurma(Turma* t, unsigned int id)
{
    this->turmas.insert(id, t);
}

QMap<unsigned int, Turma*> Disciplina::getTurmas()
{
    return this->turmas;
}

Caller function (I broke it down for easier debugging)

Disciplina*
MainWindow::getSelectedDisciplina()
{
    if(ui->disciplinaTurma->count() > 0 && currentCurso)
    {
        QMap<QString, Disciplina*> qm(currentCurso->getDisciplinas());
        QString key = ui->disciplinaTurma->itemText(ui->disciplinaTurma->currentIndex());
        Disciplina* d = qm[key];
        QMessageBox::information(this, d->getId(), d->getNome());
        return d;
    }
    else
        return NULL;
}

Solved

The Disciplina 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.

Thank you both Jacinto and sth for your help.

A: 

this can occur because you're doing it wrong.

never ping
what is he doing wrong?
ikkebr
+2  A: 

Maybe the Disciplina object you are calling getId() on was previously deleted, so that it is not valid anymore.

sth
I thought that this was impossible, but after reading Jacinto's answer about the Map and taking a look at the QMap API, I figured that this was exactly what was wrong. Since StackOverflow only accepts one answer to be selected, I picked his', but without both answers I wouldn't figure this.Thank you
Gabriel Gilini
+5  A: 

In c++'s map, if the element doesn't exist when you try to access it by its key, it just creates one for you. You are attempting to do the same thing here, and if QMap works the same way, this is what is causing your segfault.

What you should be doing is testing for the key's presence in the map before accessing it.

edit: for the C++ purists, please let me know if i have that right. I know in practice it's safer to test before accessing it, but I don't know if the phraseology of "it creates one for you" is a very good way to put it. It might just return you the space in memory where such a value would be; I don't know if it would actually call the default constructor.

San Jacinto
Yep, that nailed it, thank you :)
Gabriel Gilini
sure thing! glad to help.
San Jacinto