Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unhandled exception related to QMenuBar in widget after app exit



  • Hi,
    I have main window (QMainWindow), where I have menu. In that menu, one of options, there is action that opens other window (QWidget). In that widget in constructor I create menu bar:

    m_mainMenu = new QMenuBar();
    ui.verticalLayout_5->setMenuBar(m_mainMenu);
    m_mainMenu->addMenu("Choose object");
    

    When I don't click on that menu, everything works fine. But when I click on that menu and I want to exit (exit all app) I get unhandled exception:

    Unhandled exception at 0x52a1fbbc (qwindowsd.dll) in myApp.exe: 0xC0000005: Access violation reading location 0x00000000.

    And I get this in qscopedpointer.h in line 112

    inline T *operator->() const
    {
        Q_ASSERT(d);     //error
        return d;
    }
    

    I don't use that menuBar in other place (only in destructor I delete it), because I've just try to add it to my app. Any ideas why it happens?


  • Lifetime Qt Champion

    Hi,

    If I understand correctly, m_mainMenu is a QScopedPointer ? You shouldn't, m_mainMenu's lifetime will be handle by the widget containing verticalLayout_5. You thus have a double delete happening.

    Out of curiosity, why are you creating that menu bar ? QMainWindow already has one.



  • m_mainMenu is pointer to QMenuBar.
    I want second menu bar. One I have in my main window (that window is QMainWindow). But I have second window (it is QWidget) where I also need menu bar.


  • Lifetime Qt Champion

    m_mainMenu is a raw pointer ?

    Ok, good to know.



  • @SGaist Yes, it's raw pointer.
    Generally, I want to create some menu for users to select which camera they want use in app (for example when they have 3 cameras connected).
    I tried also QComboBox instead of QMenuBar. But after click exit button I get the same error. But it happens only when I activate that QComboBox (WMenuBar). When I don't use that widgets (I show second window but I change everything apart that QComboBox/QMenuBar) there is no error showed.

    It happens when I close whole app. I think there should be some function on stack called by that widgets but I can't find out which one.



  • Can you let parent-child take care of cleaning up instead of doing it yourself (in destructor or with QScopedPointer)?



  • @VRonin You mean set QMenuBar parent ? I can't do this with layout (as parent). I've tried set other QWidget container as a parent of my menu bar, but it nothing changed. Error still shows after exit app.

    Problem is that I have QMainWindow for example it is class named WindowOne. I have second window as QWidget named windowTwo. When I exit app. First there is called destructor of WindowOne, next destructor of WindowTwo, and then I get that error.



  • set the parent to another widget and then NEVER delete the pointer either with delete, deleteLater() or any kind of smart pointer. Use a good old raw pointer and never delete it after you assign a parent



  • @VRonin If I understand you, I've already done that. I set parent of QMenuBar to other existing widget, and I do not delete it in any possible way. But it still crashes.

    What is strange it crashes only with widgets that are pull-down or pop up list. If I create other widget problem doesn't exists.

    EDIT: Ok, I create QGroupBox with QCheckBox widgets instead QMenuBar or QComboBox and till now it works fine, so probably I stay with it. Maybe it is not the best choice, but it works.


Log in to reply