Results 1 to 6 of 6

Thread: Random crashes with dynamic tabs

  1. #1
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Random crashes with dynamic tabs

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,141
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Random crashes with dynamic tabs

    Try using "tab->deleteLater()" instead of "delete tab". This defers actual deletion until the next time the event loop runs, which allows Qt to remove the widget and clean up.

    Setting "tab = NULL" in the next line really does nothing except to set the value of your local variable that contains the pointer to NULL. It is no different from having an "int" local variable and setting its value to something. As soon as the method exits, the stack is cleaned up and all those local variables go away.

  3. The following user says thank you to d_stranz for this useful post:

    scuba (21st September 2015)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Random crashes with dynamic tabs

    The locking mechanism in your closeTab() routine is unnecessary in a single threaded program. The program cannot re-enter this routine until it processes another click event, and that cannot happen before it leaves this slot and reaches the event loop.

    Run your program in a debugger, make if fail and then walk back through the stack backtrace to find where in your code it fails from.

  5. The following user says thank you to ChrisW67 for this useful post:

    scuba (21st September 2015)

  6. #4
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Random crashes with dynamic tabs

    Quote Originally Posted by d_stranz View Post
    Try using "tab->deleteLater()" instead of "delete tab".
    Thanks, but I forgot to mention I have already tried it. The result is the same.
    Last edited by scuba; 13th September 2015 at 08:08.

  7. #5
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Random crashes with dynamic tabs

    Quote Originally Posted by ChrisW67 View Post
    Run your program in a debugger, make if fail and then walk back through the stack backtrace to find where in your code it fails from.
    Call stack trace points to QGestureManager::filterEvent (grayed out). On an other attempt it gives QCoreApplication.

    Qt Code:
    1. Starting debugger "LldbEngine" for ABI "x86-macos-generic-mach_o-64bit"...
    2. dStart parameters: 'TabCrash' mode: 1
    3. dABI: x86-macos-generic-mach_o-64bit
    4. dLanguages: c++
    5.  
    6. cut --- cut --- cut
    7.  
    8. dLldb stdout: registers=[{name="rax",value="0x000000010123ae10",type="unsigned long"},{name="rbx",value="0x000000000000007c",type="unsigned long"},{name="rcx",value="0x0000000100ff3708",type="unsigned long"},{name="rdx",value="0x00007fff5fbfe278",type="unsigned long"},{name="rdi",value="0x0000000101338f00",type="unsigned long"},{name="rsi",value="0x0000000101239700",type="unsigned long"},{name="rbp",value="0x00007fff5fbfdf70",type="unsigned long"},{name="rsp",value="0x00007fff5fbfdf20",type="unsigned long"},{name="r8",value="0x00000001091cf0d0",type="unsigned long"},{name="r9",value="0x000000000000012c",type="unsigned long"},{name="r10",value="0x00000000000003de",type="unsigned long"},{name="r11",value="0xfffffffffffffff8",type="unsigned long"},{name="r12",value="0x0000000100e4faa8",type="unsigned long"},{name="r13",value="0x0000000100e4faa8",type="unsigned long"},{name="r14",value="0x0000000101239700",type="unsigned long"},{name="r15",value="0x00000001091dd928",type="unsigned long"},{name="rip",value="0x0000000100081531",type="unsigned long"},{name="rflags",value="0x0000000000010202",type="unsigned long"},{name="cs",value="0x000000000000002b",type="unsigned long"},{name="fs",value="0x0000000000000000",type="unsigned long"},{name="gs",value="0x0000000000000000",type="unsigned long"},{name="eax",value="0x0123ae10",type="unsigned int"},{name="ebx",value="0x0000007c",type="unsigned int"},{name="ecx",value="0x00ff3708",type="unsigned int"},{name="edx",value="0x5fbfe278",type="unsigned int"},{name="edi",value="0x01338f00",type="unsigned int"},{name="esi",value="0x01239700",type="unsigned int"},{name="ebp",value="0x5fbfdf70",type="unsigned int"},{name="esp",value="0x5fbfdf20",type="unsigned int"},{name="r8d",value="0x091cf0d0",type="unsigned int"},{name="r9d",value="0x0000012c",type="unsigned int"},{name="r10d",value="0x000003de",type="unsigned int"},{name="r11d",value="0xfffffff8",type="unsigned int"},{name="r12d",value="0x00e4faa8",type="unsigned int"},{name="r13d",value="0x00e4faa8",type="unsigned int"},{name="r14d",value="0x01239700",type="unsigned int"},{name="r15d",value="0x091dd928",type="unsigned int"},{name="ax",value="0xae10",type="unsigned short"},{name="bx",value="0x007c",type="unsigned short"},{name="cx",value="0x3708",type="unsigned short"},{name="dx",value="0xe278",type="unsigned short"},{name="di",value="0x8f00",type="unsigned short"},{name="si",value="0x9700",type="unsigned short"},{name="bp",value="0xdf70",type="unsigned short"},{name="sp",value="0xdf20",type="unsigned short"},{name="r8w",value="0xf0d0",type="unsigned short"},{name="r9w",value="0x012c",type="unsigned short"},{name="r10w",value="0x03de",type="unsigned short"},{name="r11w",value="0xfff8",type="unsigned short"},{name="r12w",value="0xfaa8",type="unsigned short"},{name="r13w",value="0xfaa8",type="unsigned short"},{name="r14w",value="0x9700",type="unsigned short"},{name="r15w",value="0xd928",type="unsigned short"},{name="ah",value="0xae",type="unsigned char"},{name="bh",value="0x00",type="unsigned char"},{name="ch",value="0x37",type="unsigned char"},{name="dh",value="0xe2",type="unsigned char"},{name="al",value="0x10",type="unsigned char"},{name="bl",value="0x7c",type="unsigned char"},{name="cl",value="0x08",type="unsigned char"},{name="dl",value="0x78",type="unsigned char"},{name="dil",value="0x00",type="unsigned char"},{name="sil",value="0x00",type="unsigned char"},{name="bpl",value="0x70",type="unsigned char"},{name="spl",value="0x20",type="unsigned char"},{name="r8l",value="0xd0",type="unsigned char"},{name="r9l",value="0x2c",type="unsigned char"},{name="r10l",value="0xde",type="unsigned char"},{name="r11l",value="0xf8",type="unsigned char"},{name="r12l",value="0xa8",type="unsigned char"},{name="r13l",value="0xa8",type="unsigned char"},{name="r14l",value="0x00",type="unsigned char"},{name="r15l",value="0x28",type="unsigned char"},{name="fctrl",value="0x037f",type="unsigned short"},{name="fstat",value="0x0220",type="unsigned short"},{name="ftag",value="0x00",type="unsigned char"},{name="fop",value="0x0000",type="unsigned short"},{name="fioff",value="0x8d88e3b3",type="unsigned int"},{name="fiseg",value="0x0000",type="unsigned short"},{name="fooff",value="0x5fbff0e0",type="unsigned int"},{name="foseg",value="0x0000",type="unsigned short"},{name="mxcsr",value="0x00001fa1",type="unsigned int"},{name="mxcsrmask",value="0x0000ffff",type="unsigned int"},{name="stmm0",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm1",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm2",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm3",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm4",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm5",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm6",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="stmm7",value="None",type="unsigned char __attribute__((ext_vector_type(10)))"},{name="ymm0",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm1",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm2",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm3",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm4",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm5",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm6",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm7",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm8",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm9",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm10",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm11",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm12",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm13",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm14",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="ymm15",value="None",type="unsigned char __attribute__((ext_vector_type(32)))"},{name="xmm0",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm1",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm2",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm3",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm4",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm5",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm6",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm7",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm8",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm9",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm10",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm11",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm12",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm13",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm14",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="xmm15",value="None",type="unsigned char __attribute__((ext_vector_type(16)))"},{name="trapno",value="0x0000000e",type="unsigned int"},{name="err",value="0x00000004",type="unsigned int"},{name="faultvaddr",value="0x0000000000000084",type="unsigned long"},]@
    To copy to clipboard, switch view to plain text mode 

    Now I'm wondering, if the crashes happen because I am accidentally making some gesture with MacBooks trackpad when doing the fast clicks. I tried before to crash the program with a opentab-closetab -loop, but that never succeeded. Maybe it's a problem with gestures?

    EDIT: This might be related: https://bugreports.qt.io/browse/QTBUG-46264
    Last edited by scuba; 13th September 2015 at 12:27.

  8. #6
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Random crashes with dynamic tabs

    Upgrading to Qt 5.5.0 solved the issue.

Similar Threads

  1. Replies: 1
    Last Post: 12th June 2014, 07:27
  2. semi-random crashes in QByteArray::data()
    By tuli in forum Qt Programming
    Replies: 10
    Last Post: 27th May 2013, 22:24
  3. Replies: 4
    Last Post: 21st October 2012, 19:04
  4. How to debug random crashes?
    By Gunnar in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2011, 21:25
  5. Replies: 1
    Last Post: 7th April 2010, 16:26

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.