Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unhandled exception related to QMenuBar in widget after app exit

Unhandled exception related to QMenuBar in widget after app exit

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    never_ever
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        never_ever
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          m_mainMenu is a raw pointer ?

          Ok, good to know.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply
          0
          • SGaistS SGaist

            m_mainMenu is a raw pointer ?

            Ok, good to know.

            N Offline
            N Offline
            never_ever
            wrote on last edited by never_ever
            #5

            @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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              N 1 Reply Last reply
              0
              • VRoninV VRonin

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

                N Offline
                N Offline
                never_ever
                wrote on last edited by never_ever
                #7

                @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.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  N 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    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

                    N Offline
                    N Offline
                    never_ever
                    wrote on last edited by never_ever
                    #9

                    @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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved