QSound global object bug

  • This is a fresh project, only lines added are lines 4 and 5.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSound>
    QSound notif("notif.wav");
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        delete ui;

    I've added QT= multimedia in the .pro file and am sure that "notif.wav" is in the right folder. When compiling, I get some strange errors: When I first click on run, the window opens, and I don't get any error. I close the program and click again run, I get runtime error: "Debug Error ! R6010 -abort() has been called". I click on abort, click again on run, and this time I get output "test.exe exited with code 255". Click one more time on run, an get output "The program has unexpectedly finished" "test.exe" crashed.

    This is the output when I get the runtime error:
    QThread::wait: Thread tried to wait on itself
    QCoreApplication::postEvent: Unexpected null receiver
    QThread: Destroyed while thread is still running
    QMutex: destroying locked mutex
    QMutex: destroying locked mutex
    C:\Users\User\Documents\build-test2-Desktop_Qt_5_7_0_MSVC2013_64bit-Debug\debug\test2.exe exited with code 3."

    So I get 4 different error without changing anything in the code. What's wrong ?

  • Hi! QSound inherits from QObject. You must not create any QObject before the QApplication object has been created. Also all QObjects must be destroyed before the QApplication object gets destroyed. In other words: Don't use global / static QObjects or you'll experience undefined behaviour.

  • @Wieland
    Oh, thanks, didn't knew that.

Log in to reply