Results 1 to 2 of 2

Thread: Program crashing when trying to delete children window

  1. #1
    Join Date
    Dec 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Program crashing when trying to delete children window

    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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "playerwindow.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. PlayerWindow *playerWindow;
    22.  
    23. void keyPressEvent(QKeyEvent *event);
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <qdebug.h>
    4. #include <QKeyEvent>
    5. #include <VLCQtCore/Common.h>
    6.  
    7. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. qDebug() << "destructed main";
    16. delete playerWindow;
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::keyPressEvent(QKeyEvent *event)
    21. {
    22. switch (event->key()) {
    23. case Qt::Key_Return: case Qt::Key_Enter:
    24. playerWindow = new PlayerWindow(this);
    25. playerWindow->show();
    26.  
    27. break;
    28. case Qt::Key_Escape:
    29. this->close();
    30.  
    31. break;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    playerwindow.h
    Qt Code:
    1. #ifndef PLAYERWINDOW_H
    2. #define PLAYERWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class PlayerWindow;
    8. }
    9.  
    10. class VlcInstance;
    11. class VlcMedia;
    12. class VlcMediaPlayer;
    13.  
    14. class PlayerWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit PlayerWindow(QWidget *parent = 0);
    20. ~PlayerWindow();
    21.  
    22. private slots:
    23. void openFile();
    24.  
    25. private:
    26. Ui::PlayerWindow *ui;
    27.  
    28. VlcInstance *_instance;
    29. VlcMedia *_media;
    30. VlcMediaPlayer *_player;
    31.  
    32. void keyPressEvent(QKeyEvent *event);
    33. };
    34.  
    35. #endif // PLAYERWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    playerwindow.cpp
    Qt Code:
    1. #include "playerwindow.h"
    2. #include "ui_playerwindow.h"
    3.  
    4. #include <VLCQtCore/Common.h>
    5. #include <VLCQtCore/Instance.h>
    6. #include <VLCQtCore/Media.h>
    7. #include <VLCQtCore/MediaPlayer.h>
    8. #include <QKeyEvent>
    9. #include <QDebug>
    10.  
    11. PlayerWindow::PlayerWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::PlayerWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. _instance = new VlcInstance(VlcCommon::args(), this);
    16. _player = new VlcMediaPlayer(_instance);
    17. _player->setVideoWidget(ui->video);
    18.  
    19. ui->video->setMediaPlayer(_player);
    20. ui->seek->setMediaPlayer(_player);
    21. }
    22.  
    23.  
    24. PlayerWindow::~PlayerWindow()
    25. {
    26. qDebug() << "destructed player";
    27. delete _player;
    28. delete _media;
    29. delete _instance;
    30.  
    31. delete ui;
    32. }
    33.  
    34. void PlayerWindow::openFile()
    35. {
    36. QString file = "/home/test/Video/VID_20160822_124845.mp4";
    37. _media = new VlcMedia(file, true, _instance);
    38. _player->open(_media);
    39. }
    40.  
    41. void PlayerWindow::keyPressEvent(QKeyEvent *event)
    42. {
    43. if(event->key() == Qt::Key_Escape){
    44. _player->stop();
    45. this->hide();
    46. }
    47. else if(event->key() == Qt::Key_Space){
    48. openFile();
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program crashing when trying to delete children window

    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,
    _

Similar Threads

  1. CDB Debugger makes my Qt program crashing instantly
    By reezeus in forum Installation and Deployment
    Replies: 0
    Last Post: 24th September 2015, 12:17
  2. QDomNode delete children
    By trallallero in forum Qt Programming
    Replies: 9
    Last Post: 11th December 2013, 01:13
  3. Replies: 3
    Last Post: 27th October 2011, 13:29
  4. program crashing randomly code -1073741819
    By feraudyh in forum General Programming
    Replies: 6
    Last Post: 21st September 2010, 18:07
  5. Replies: 3
    Last Post: 8th September 2008, 19:18

Tags for this Thread

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.