Search code examples
c++qtvlc-qt

Qt app crashes after declaring a new object


I faced very strange problem.I am using VLCQt library and successfully run a very simple videoplayer. but when I want to add a very simple Qlabel to the main class, It crashes at this point ui->setupUi(this). the output window contains these statament:

HEAP[VideoPlayer.exe]: Invalid address specified to RtlValidateHeap( 00000000002F0000, 0000000000334220 ) VideoPlayer.exe has triggered a breakpoint.

SimplePlayer.h:

class SimplePlayer : public QMainWindow
{
     Q_OBJECT

    public:
    SimplePlayer(QWidget *parent = 0);
    ~SimplePlayer();

    private:
    Ui::SimplePlayer *ui;
    VlcInstance *_instance;
    VlcMedia *_media;
    VlcMediaPlayer *_player;
    //QLabel *_lbl;//  if I declare a very simple Qlabel the app crashes

    private slots:
    void openLocal();
    void openUrl();
 };

SimplePlayer.cpp

  SimplePlayer::SimplePlayer(QWidget *parent)
: QMainWindow(parent)
{
    ui->setupUi(this);
   _instance = new VlcInstance(VlcCommon::args(), this);
   _player = new VlcMediaPlayer(_instance);
   _player->setVideoWidget(ui->video);
    ui->video->setMediaPlayer(_player);
    ui->volume->setMediaPlayer(_player);
    ui->volume->setVolume(50);
    ui->seek->setMediaPlayer(_player);
   // _lbl = new QLabel;//  if I declare a very simple Qlabel the app crashes
    ...//connections

}

Solution

  • You are never setting your ui class member, so you are calling setupUi(this); on a null pointer.

    You need to either make your member a value and not a pointer:

    private:
        Ui::SimplePlayer ui;
    

    Or you need to create a SimplePlayer at the beginning of your constructor:

    ui = new Ui::SimplePlayer();