Hi,

I am working on an application, where tabs are opened and closed dynamically - just like in webbrowsers. The problem is that, I keep experiencing random but regular crashes when closing a tab. I made a very short piece of code to demonstrate the problem:

Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QtWidgets>
  6. #include <mytab.h>
  7.  
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11.  
  12. QWidget *central;
  13.  
  14. QListWidget *listWidget;
  15.  
  16. QTabWidget *tabWidget;
  17.  
  18. QGridLayout *layout;
  19.  
  20. bool deleteBusy = false;
  21.  
  22. public:
  23. MainWindow(QWidget *parent = 0);
  24. ~MainWindow();
  25.  
  26. public slots:
  27.  
  28. void newTab();
  29. void closeTab(int);
  30.  
  31. };
  32.  
  33. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. #ifndef MYTAB_H
  2. #define MYTAB_H
  3.  
  4. #include <QWidget>
  5. #include <QtWidgets>
  6.  
  7.  
  8. class myTab : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit myTab(QWidget *parent = 0);
  13.  
  14.  
  15. QTableWidget *tableWidget;
  16. QTableWidget *tableWidget2;
  17. QGridLayout *layout;
  18.  
  19. signals:
  20.  
  21. public slots:
  22.  
  23. };
  24.  
  25. #endif // MYTAB_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "mainwindow.h"
  2.  
  3.  
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent)
  6. {
  7.  
  8. central = new QWidget(this);
  9. setCentralWidget(central);
  10.  
  11. listWidget = new QListWidget();
  12.  
  13. for (int a = 0; a < 14 ; a++) // Just to make the listWidget easier to click
  14. {
  15. new QListWidgetItem(tr("Click here"), listWidget);
  16. }
  17.  
  18.  
  19. tabWidget = new QTabWidget();
  20.  
  21. layout = new QGridLayout(central);
  22. layout->addWidget(listWidget, 0, 0);
  23. layout->addWidget(tabWidget, 1, 0);
  24.  
  25. tabWidget->addTab(new myTab, tr("Tab"));
  26.  
  27. QObject::connect(listWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(newTab()) );
  28. QObject::connect(tabWidget, SIGNAL(tabBarClicked(int)), this, SLOT(closeTab(int)) );
  29.  
  30.  
  31. }
  32.  
  33. MainWindow::~MainWindow()
  34. {
  35.  
  36. }
  37.  
  38. void MainWindow::newTab()
  39. {
  40.  
  41. tabWidget->addTab(new myTab, tr("Tab"));
  42.  
  43. }
  44.  
  45. void MainWindow::closeTab(int)
  46. {
  47.  
  48. if (deleteBusy == true)
  49. QMessageBox::information(0, "Information", "Another tab is closing!" );
  50. deleteBusy = true;
  51.  
  52. if (tabWidget->count() < 1)
  53. QMessageBox::information(0, "Information", "No tabs!" );
  54.  
  55. QWidget *tab = tabWidget->currentWidget();
  56.  
  57. tabWidget->removeTab(tabWidget->currentIndex());
  58.  
  59. if (tab != NULL)
  60. {
  61. delete tab;
  62. tab = NULL;
  63. }
  64. else
  65. {
  66. QMessageBox::information(0, "Information", "Tab is null!" );
  67. }
  68.  
  69.  
  70. deleteBusy = false;
  71.  
  72. }
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. #include "mytab.h"
  2.  
  3. myTab::myTab(QWidget *parent) :
  4. QWidget(parent)
  5. {
  6.  
  7. tableWidget = new QTableWidget();
  8. tableWidget2 = new QTableWidget();
  9.  
  10. layout = new QGridLayout(this);
  11. layout->addWidget(tableWidget, 0, 0);
  12. layout->addWidget(tableWidget2, 0, 1);
  13.  
  14. for (int a = 0; a<1000; a++) // Adding some content to the tab...
  15. {
  16. tableWidget->insertRow(0);
  17. tableWidget2->insertRow(0);
  18. }
  19.  
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 


If I VERY quickly open 3-4 tabs and close them, and then reopen, etc, the program will sometimes crash (about 1 case from 4). In my actual application the problem is more evident. Any ideas why this is happening? Suspecting some kind on heap/stack problem, but as far as I understand it, I can't point the error.