PDA

View Full Version : Program crashing when trying to delete children window



umberto.palazzini
4th January 2017, 12:57
Hi all,
my program is crashing when destroying its children. I've noticed that is first deletes itself and then its children, but I can't find out the solution

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "playerwindow.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
PlayerWindow *playerWindow;

void keyPressEvent(QKeyEvent *event);
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <QKeyEvent>
#include <VLCQtCore/Common.h>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}


MainWindow::~MainWindow()
{
qDebug() << "destructed main";
delete playerWindow;
delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Return: case Qt::Key_Enter:
playerWindow = new PlayerWindow(this);
playerWindow->show();

break;
case Qt::Key_Escape:
this->close();

break;
}
}


playerwindow.h


#ifndef PLAYERWINDOW_H
#define PLAYERWINDOW_H

#include <QMainWindow>

namespace Ui {
class PlayerWindow;
}

class VlcInstance;
class VlcMedia;
class VlcMediaPlayer;

class PlayerWindow : public QMainWindow
{
Q_OBJECT

public:
explicit PlayerWindow(QWidget *parent = 0);
~PlayerWindow();

private slots:
void openFile();

private:
Ui::PlayerWindow *ui;

VlcInstance *_instance;
VlcMedia *_media;
VlcMediaPlayer *_player;

void keyPressEvent(QKeyEvent *event);
};

#endif // PLAYERWINDOW_H

playerwindow.cpp


#include "playerwindow.h"
#include "ui_playerwindow.h"

#include <VLCQtCore/Common.h>
#include <VLCQtCore/Instance.h>
#include <VLCQtCore/Media.h>
#include <VLCQtCore/MediaPlayer.h>
#include <QKeyEvent>
#include <QDebug>

PlayerWindow::PlayerWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::PlayerWindow)
{
ui->setupUi(this);

_instance = new VlcInstance(VlcCommon::args(), this);
_player = new VlcMediaPlayer(_instance);
_player->setVideoWidget(ui->video);

ui->video->setMediaPlayer(_player);
ui->seek->setMediaPlayer(_player);
}


PlayerWindow::~PlayerWindow()
{
qDebug() << "destructed player";
delete _player;
delete _media;
delete _instance;

delete ui;
}

void PlayerWindow::openFile()
{
QString file = "/home/test/Video/VID_20160822_124845.mp4";
_media = new VlcMedia(file, true, _instance);
_player->open(_media);
}

void PlayerWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape){
_player->stop();
this->hide();
}
else if(event->key() == Qt::Key_Space){
openFile();
}
}

anda_skoa
4th January 2017, 15:24
The most obvious mistake is of course that the "playerWindow" variable isn't initialized so if you ever delete that window with having pressed enter you will call delete on an invalid pointer.

It is also not clear why you are calling delete on playerWindow at all. If it is not created there is not need to delete it, if it has been created, its parent will delete it.

Some for a couple of members of the second class.

In any case, since you have passed _player to two other objects, you should either set a null pointer on them instead before deleting _player or deleting _player after all its usages have been deleted.

Cheers,
_