PDA

View Full Version : Program Crashing because of Singal [need help!]



21altf
27th October 2011, 05:48
I have a program that works fine without signals, but when I add them I start having trouble.

Basecally, I'm building a QSplashScreen for this program and since this is a short program it doesnt hold very long. So I Tryed to create an alternative class with a especific slot for terminating this program using QTimer::start(ms);

the problem is, I dont want my main widget to start b4 this QSpalshScreen is closed. So I added a Signal, thatway when I terminate the QSplashScreen I would emit a signal and my QMainWindow would attach it to SLOT(show()).

Whats happening is: When the QSplashScreen terminates the program crashes with this error 255.

my program is:

--QSS.h--
#ifndef QSS_H
#define QSS_H
#include <QSplashScreen>
#include <QTimer>
#include <QWidget>
#include <QObject>
#include <QtGui>

class QMainWindow;

class QSS: public QSplashScreen {

Q_OBJECT

public:
explicit QSS(QSplashScreen *parent = 0);
void criarTimer();

public slots:
void fechar();

signals:
void fechou();
};

#endif // QSS_H


--QSS.cpp--

#include <QSS.h>
#include <QWidget>
#include <QObject>
#include <QSplashScreen>

QSS::QSS(QSplashScreen *parent) :
QSplashScreen(parent)
{
}

//slots
void QSS::fechar()
{
this->~QSplashScreen();
emit fechou();
}

//funções públicas
void QSS::criarTimer()
{
QTimer *tempo = new QTimer;
tempo->setSingleShot(true);
QObject::connect(tempo,SIGNAL(timeout()),this,SLOT (fechar()));
tempo->start(3000);

}


---main.cpp---

#include <QtGui/QApplication>
#include <QSplashScreen>
#include <QSS.h>
#include "teste3mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSS *splash = new QSS;

splash->setPixmap(QPixmap(":/images/circulo.png"));
splash->show();

Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up..."),topRight,Qt::black);


teste3MainWindow w;
splash->criarTimer();
QObject::connect(splash,SIGNAL(fechou()),&w,SLOT(show()));




return a.exec();
}


Tnx

nix
27th October 2011, 08:32
That's normal after deleting the QSplashScreen base you should not do any stuff with signal or metadata.

Why don't do that :


//slots
void QSS::fechar()
{
hide();
emit fechou();
close();
}
and add a Qt::WA_DeleteOnClose flag on your splashscreen (or use deleteLater() ).

And please, use CODE TAG!!

ChrisW67
27th October 2011, 11:21
//slots
void QSS::fechar()
{
this->~QSplashScreen();
emit fechou();
}

You are destroying the QSplashScreen underpinning your derived QSS splash screen and then trying to use the object. It is hardly surprising this crashes. I don't know of any circumstance under which you would call a destructor directly.

Take nix's advice including the Qt::WA_DeleteOnClose flag to avoid the resource leak you have at the moment (or put it on the stack). You are also leaking memory by allocating a QTimer on the heap, not setting a parent, and then discarding the pointer to it. No need to create a timer explicitly. Just use the QTimer::singleShot() static function.

21altf
27th October 2011, 13:29
Thanks for the tip!

Both advices where very good and it does just what is meant too.

And sorry, I didn't know how to use code tag... I'll learn for next time.

#THIS IS ME EDITING THIS POST. Searched and now its good to go with the CodeTag. Thanks!


--QSS.h--
#ifndef QSS_H
#define QSS_H
#include <QSplashScreen>
#include <QTimer>
#include <QWidget>
#include <QObject>
#include <QtGui>

class QMainWindow;

class QSS: public QSplashScreen {

Q_OBJECT

public:
explicit QSS(QSplashScreen *parent = 0);
void criarTimer();

public slots:
void fechar();

signals:
void fechou();
};

#endif // QSS_H