Results 1 to 4 of 4

Thread: Program Crashing because of Singal [need help!]

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Exclamation Program Crashing because of Singal [need help!]

    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(s how()));




    return a.exec();
    }


    Tnx

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Program Crashing because of Singal [need help!]

    That's normal after deleting the QSplashScreen base you should not do any stuff with signal or metadata.

    Why don't do that :

    Qt Code:
    1. //slots
    2. void QSS::fechar()
    3. {
    4. hide();
    5. emit fechou();
    6. close();
    7. }
    To copy to clipboard, switch view to plain text mode 
    and add a Qt::WA_DeleteOnClose flag on your splashscreen (or use deleteLater() ).

    And please, use CODE TAG!!

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Program Crashing because of Singal [need help!]

    Qt Code:
    1. //slots
    2. void QSS::fechar()
    3. {
    4. this->~QSplashScreen();
    5. emit fechou();
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Default Re: Program Crashing because of Singal [need help!]

    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!

    Qt Code:
    1. --QSS.h--
    2. #ifndef QSS_H
    3. #define QSS_H
    4. #include <QSplashScreen>
    5. #include <QTimer>
    6. #include <QWidget>
    7. #include <QObject>
    8. #include <QtGui>
    9.  
    10.  
    11. class QSS: public QSplashScreen {
    12.  
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit QSS(QSplashScreen *parent = 0);
    17. void criarTimer();
    18.  
    19. public slots:
    20. void fechar();
    21.  
    22. signals:
    23. void fechou();
    24. };
    25.  
    26. #endif // QSS_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Crashing without debug mode- No crashing with gdb debugger
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2011, 11:27
  2. Replies: 12
    Last Post: 9th January 2011, 13:51
  3. program crashing randomly code -1073741819
    By feraudyh in forum General Programming
    Replies: 6
    Last Post: 21st September 2010, 17:07
  4. Debugging singal-slot connections
    By gnik in forum Qt-based Software
    Replies: 0
    Last Post: 11th July 2009, 19:50
  5. singal send twice?
    By masoroso in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2007, 13:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.