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. Qt 6.6.0 Destructor of QList causes a crash.

Qt 6.6.0 Destructor of QList causes a crash.

Scheduled Pinned Locked Moved Solved General and Desktop
19 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.
  • H Offline
    H Offline
    human99
    wrote on 15 Dec 2023, 23:53 last edited by
    #1

    I was writing some code to explore using theQTreeWidget class and I'm getting a crash in the destructor of a QList:

    I'm using Qt version 6.6.0 with MSVC 2019, building for Debug x64:

    It's a similar issue as posted in here: https://forum.qt.io/topic/151502/my-qt-6-6-0-code-crashs-in-debug-mode-qarraydatapointer-and-not-on-release-on-windows

    This is my entire program

        QApplication app(argc, argv);
    	QTreeWidget* treeWidget = new QTreeWidget();
    	treeWidget->setColumnCount(1);
    	QList<QTreeWidgetItem*> items;
    	for (int i = 0; i < 10; ++i)
    	{
    		items.append(new QTreeWidgetItem(treeWidget, QStringList(QString("item: %1").arg(1))));
    	}
    
    	treeWidget->insertTopLevelItems(0, items);
    	treeWidget->show();
    
        return app.exec();
    

    The issue happens right after

    items.append(new QTreeWidgetItem(treeWidget, QStringList(QString("item: %1").arg(1))));
    

    whereTheBreakPointIs.JPG callstackissue.JPG breakpoint.JPG

    If I modify my program and move the QStringList into a local variable called 'qstlist' declared within the main function's scope, the program runs just fine. But of course as soon as I exit the GUI and the exec function finishes, the destructor still gets called to clean up the local variable and I do still get the same error, but at least I get to see the GUI before that point.

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    	QTreeWidget* treeWidget = new QTreeWidget();
    	treeWidget->setColumnCount(1);
    	QList<QTreeWidgetItem*> items;
    	QStringList qstlist = QStringList(QString("item: %1").arg(1));
    	for (int i = 0; i < 10; ++i)
    	{
    		items.append(new QTreeWidgetItem(treeWidget, qstlist));
    	}
    
    	treeWidget->insertTopLevelItems(0, items);
    	treeWidget->show();
    
        return app.exec();
    }
    

    working.JPG

    1 Reply Last reply
    0
    • H Offline
      H Offline
      human99
      wrote on 16 Dec 2023, 00:13 last edited by
      #2

      Of course, I clasically forgot to mention the most important detail:

      If I continue through the breakpoint this is the specific error that I receive from windows:

      DebugAssertion.JPG

      Seems to me that for some reason the destructor of the QStringList is trying to deallocate memory that isn't pointing to an actual block on the Heap. Any ideas?

      C 1 Reply Last reply 16 Dec 2023, 08:11
      0
      • H human99
        16 Dec 2023, 00:13

        Of course, I clasically forgot to mention the most important detail:

        If I continue through the breakpoint this is the specific error that I receive from windows:

        DebugAssertion.JPG

        Seems to me that for some reason the destructor of the QStringList is trying to deallocate memory that isn't pointing to an actual block on the Heap. Any ideas?

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 16 Dec 2023, 08:11 last edited by
        #3

        @human99 From my Pic you are mixing debug and release libs somewhere. Please show me your CMakeLists.txt

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        H 1 Reply Last reply 16 Dec 2023, 09:32
        1
        • C Christian Ehrlicher
          16 Dec 2023, 08:11

          @human99 From my Pic you are mixing debug and release libs somewhere. Please show me your CMakeLists.txt

          H Offline
          H Offline
          human99
          wrote on 16 Dec 2023, 09:32 last edited by
          #4

          @Christian-Ehrlicher

          This is the CMakeLists.txt, I just took it from the calculator example from the examples folder within QtBase. I copy pasted the whole calculator example to a different folder and built from there.

          I'm not actually using the calculator code at all though. I cut all the code out to just test out the QTreeWidget class. I could only keep main.cpp and the app would work the same.

          # Copyright (C) 2022 The Qt Company Ltd.
          # SPDX-License-Identifier: BSD-3-Clause
          
          cmake_minimum_required(VERSION 3.16)
          project(calculator LANGUAGES CXX)
          
          if(NOT DEFINED INSTALL_EXAMPLESDIR)
              set(INSTALL_EXAMPLESDIR "examples")
          endif()
          
          set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/widgets/calculator")
          
          find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
          
          qt_standard_project_setup()
          
          qt_add_executable(calculator
              button.cpp button.h
              calculator.cpp calculator.h
              my_tree.cpp my_tree.h
              main.cpp
          )
          
          set_target_properties(calculator PROPERTIES
              WIN32_EXECUTABLE TRUE
              MACOSX_BUNDLE TRUE
          )
          
          target_link_libraries(calculator PRIVATE
              Qt6::Core
              Qt6::Gui
              Qt6::Widgets
          )
          
          install(TARGETS calculator
              RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
              BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
              LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
          )
          
          
          1 Reply Last reply
          0
          • H Offline
            H Offline
            human99
            wrote on 16 Dec 2023, 09:48 last edited by
            #5

            @Christian-Ehrlicher What do you mean mixing debug and release libs? As in, I'm linking with Qt libs compiled for debug/release at the same time?

            If that's the case I've only ever compiled Qt6 once for Release x64.

            J 1 Reply Last reply 16 Dec 2023, 09:56
            0
            • H human99
              16 Dec 2023, 09:48

              @Christian-Ehrlicher What do you mean mixing debug and release libs? As in, I'm linking with Qt libs compiled for debug/release at the same time?

              If that's the case I've only ever compiled Qt6 once for Release x64.

              J Offline
              J Offline
              JonB
              wrote on 16 Dec 2023, 09:56 last edited by
              #6

              @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

              If that's the case I've only ever compiled Qt6 once for Release x64.

              Then you must not build your own application for Debug, as you say you have. Everything --- your application and any libraries it uses, including Qt --- must all be compiled for only one of Debug or Release. You would need to compile your Qt6 for Debug if you want to do the same for your program

              I believe MSVC/Windows runtime C++ libraries make it so it is possible to mix these during a compilation/link, and if so that causes such problems as you say, weird crashes.

              H 2 Replies Last reply 16 Dec 2023, 10:28
              1
              • J JonB
                16 Dec 2023, 09:56

                @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                If that's the case I've only ever compiled Qt6 once for Release x64.

                Then you must not build your own application for Debug, as you say you have. Everything --- your application and any libraries it uses, including Qt --- must all be compiled for only one of Debug or Release. You would need to compile your Qt6 for Debug if you want to do the same for your program

                I believe MSVC/Windows runtime C++ libraries make it so it is possible to mix these during a compilation/link, and if so that causes such problems as you say, weird crashes.

                H Offline
                H Offline
                human99
                wrote on 16 Dec 2023, 10:28 last edited by
                #7

                @JonB
                Thank you very much for the answer, I had no idea that was the case.

                So my by mixing debug and release what is meant is literally don't build your own application in debug with Qt release libs or vice versa.

                J 1 Reply Last reply 16 Dec 2023, 10:42
                0
                • J JonB
                  16 Dec 2023, 09:56

                  @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                  If that's the case I've only ever compiled Qt6 once for Release x64.

                  Then you must not build your own application for Debug, as you say you have. Everything --- your application and any libraries it uses, including Qt --- must all be compiled for only one of Debug or Release. You would need to compile your Qt6 for Debug if you want to do the same for your program

                  I believe MSVC/Windows runtime C++ libraries make it so it is possible to mix these during a compilation/link, and if so that causes such problems as you say, weird crashes.

                  H Offline
                  H Offline
                  human99
                  wrote on 16 Dec 2023, 10:41 last edited by
                  #8

                  @JonB Or I suppose more generally, this isn't QT specific information: Everything must be compiled for the same 'platform'

                  1 Reply Last reply
                  0
                  • H human99
                    16 Dec 2023, 10:28

                    @JonB
                    Thank you very much for the answer, I had no idea that was the case.

                    So my by mixing debug and release what is meant is literally don't build your own application in debug with Qt release libs or vice versa.

                    J Offline
                    J Offline
                    JonB
                    wrote on 16 Dec 2023, 10:42 last edited by JonB
                    #9

                    @human99
                    Exactly that. I don't quite know how in your situation all the stuff successfully links when some is debug-only and some is release-only, I don't know if that is something MSVC permits, but it doesn't sound good. All of one kind or another, then see if your bug occurs.

                    H 2 Replies Last reply 16 Dec 2023, 10:55
                    1
                    • J JonB
                      16 Dec 2023, 10:42

                      @human99
                      Exactly that. I don't quite know how in your situation all the stuff successfully links when some is debug-only and some is release-only, I don't know if that is something MSVC permits, but it doesn't sound good. All of one kind or another, then see if your bug occurs.

                      H Offline
                      H Offline
                      human99
                      wrote on 16 Dec 2023, 10:55 last edited by
                      #10

                      @JonB
                      Thank you very much for the info. I built the app for release and it works perfectly fine. As you could tell I'm a bit new to system's languages so I never knew about this.

                      May I ask how do you automate this with CMake?

                      I've compiled QT for x64 Release and installed it in C:\qt6. I imagine if I wanted to build my app for debug I'd need to recompile QT for x64 debug and install it where?

                      I can install it in some other arbitrary path and figure out how to manually get this working but is there any sensible way to install it such that a CMake flag would automatically build with the appropriate qt version?

                      J C 2 Replies Last reply 16 Dec 2023, 10:59
                      0
                      • H human99
                        16 Dec 2023, 10:55

                        @JonB
                        Thank you very much for the info. I built the app for release and it works perfectly fine. As you could tell I'm a bit new to system's languages so I never knew about this.

                        May I ask how do you automate this with CMake?

                        I've compiled QT for x64 Release and installed it in C:\qt6. I imagine if I wanted to build my app for debug I'd need to recompile QT for x64 debug and install it where?

                        I can install it in some other arbitrary path and figure out how to manually get this working but is there any sensible way to install it such that a CMake flag would automatically build with the appropriate qt version?

                        J Offline
                        J Offline
                        JonB
                        wrote on 16 Dec 2023, 10:59 last edited by
                        #11

                        @human99 I don't build Qt, use CMake or run on Windows. You will want to build Qt for both Debug & Release, but you'll have to wait for someone else to answer.

                        1 Reply Last reply
                        1
                        • J JonB
                          16 Dec 2023, 10:42

                          @human99
                          Exactly that. I don't quite know how in your situation all the stuff successfully links when some is debug-only and some is release-only, I don't know if that is something MSVC permits, but it doesn't sound good. All of one kind or another, then see if your bug occurs.

                          H Offline
                          H Offline
                          human99
                          wrote on 16 Dec 2023, 13:40 last edited by
                          #12

                          @JonB Yeah you're right there's some funky business going on since now that I built the debug version all the .dlls and .libs names end with a d to differenciate them from their release counterparts.

                          Why my debug build before was linking to release libraries is beyond me. Then again I am calling cmake through visual studio so I can imagine something being caused by that.

                          1 Reply Last reply
                          0
                          • H human99
                            16 Dec 2023, 10:55

                            @JonB
                            Thank you very much for the info. I built the app for release and it works perfectly fine. As you could tell I'm a bit new to system's languages so I never knew about this.

                            May I ask how do you automate this with CMake?

                            I've compiled QT for x64 Release and installed it in C:\qt6. I imagine if I wanted to build my app for debug I'd need to recompile QT for x64 debug and install it where?

                            I can install it in some other arbitrary path and figure out how to manually get this working but is there any sensible way to install it such that a CMake flag would automatically build with the appropriate qt version?

                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 16 Dec 2023, 14:10 last edited by
                            #13

                            @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                            I'd need to recompile QT for x64 debug and install it where?

                            In the same place.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            H 1 Reply Last reply 16 Dec 2023, 22:23
                            0
                            • C Christian Ehrlicher
                              16 Dec 2023, 14:10

                              @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                              I'd need to recompile QT for x64 debug and install it where?

                              In the same place.

                              H Offline
                              H Offline
                              human99
                              wrote on 16 Dec 2023, 22:23 last edited by
                              #14

                              @Christian-Ehrlicher
                              I'm compiling on windows so do you mean that when I use configure.bat I provide the same folder as an argument to the
                              -prefix flag. And build twice, once for debug, once for release and install them to the same directory?

                              I've also seen the -debug-and-release option for configure.bat which seems like it was made for this but unfortunately I'm getting an error when using that flag.

                              C 1 Reply Last reply 17 Dec 2023, 06:57
                              0
                              • H human99
                                16 Dec 2023, 22:23

                                @Christian-Ehrlicher
                                I'm compiling on windows so do you mean that when I use configure.bat I provide the same folder as an argument to the
                                -prefix flag. And build twice, once for debug, once for release and install them to the same directory?

                                I've also seen the -debug-and-release option for configure.bat which seems like it was made for this but unfortunately I'm getting an error when using that flag.

                                C Online
                                C Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 17 Dec 2023, 06:57 last edited by
                                #15

                                @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                                And build twice, once for debug, once for release and install them to the same directory?

                                In the same place.

                                What else should this mean?

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                H 1 Reply Last reply 17 Dec 2023, 11:40
                                0
                                • C Christian Ehrlicher
                                  17 Dec 2023, 06:57

                                  @human99 said in Qt 6.6.0 Destructor of QList causes a crash.:

                                  And build twice, once for debug, once for release and install them to the same directory?

                                  In the same place.

                                  What else should this mean?

                                  H Offline
                                  H Offline
                                  human99
                                  wrote on 17 Dec 2023, 11:40 last edited by
                                  #16

                                  @Christian-Ehrlicher

                                  From the point of view of a beginner that's not familiar with building code it's unintuitive. I assumed the install directory would need to be empty. Thank you very much for the input, both of you solved my problem.

                                  C 1 Reply Last reply 17 Dec 2023, 12:36
                                  0
                                  • H human99 has marked this topic as solved on 17 Dec 2023, 11:41
                                  • H human99
                                    17 Dec 2023, 11:40

                                    @Christian-Ehrlicher

                                    From the point of view of a beginner that's not familiar with building code it's unintuitive. I assumed the install directory would need to be empty. Thank you very much for the input, both of you solved my problem.

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 17 Dec 2023, 12:36 last edited by
                                    #17

                                    @human99 When you follow https://doc.qt.io/qt-6/windows-building.html then debug and release is built in one step. When you followed it and did only get the release libs then it's a bug on Qt side. Otherwise you did something different without known what to do.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    H 1 Reply Last reply 17 Dec 2023, 15:15
                                    1
                                    • C Christian Ehrlicher
                                      17 Dec 2023, 12:36

                                      @human99 When you follow https://doc.qt.io/qt-6/windows-building.html then debug and release is built in one step. When you followed it and did only get the release libs then it's a bug on Qt side. Otherwise you did something different without known what to do.

                                      H Offline
                                      H Offline
                                      human99
                                      wrote on 17 Dec 2023, 15:15 last edited by
                                      #18

                                      @Christian-Ehrlicher Thanks for the extra info. I followed https://wiki.qt.io/Building_Qt_6_from_Git, and looking at the link you provided, it outlines the same steps. The only difference being that the link I followed also shows how to get the sources from the qt git repo.

                                      I don't know if it's a bug on QT's side, I suppose it would be if the build's default behaviour should be to build both debug and release libraries.

                                      To be specific: After I ran configure.bat, the batch script's output to the commandline outlines your build options:

                                      Build options:
                                      Mode ................................... release
                                      ...
                                      

                                      And in my case the mode was set to release. And as a sanity check looking in the install folder, the names of all of the .dlls and .lib files do not end with d, so they're definitely the release binary files.

                                      There is a flag you could pass to configure.bat called -debug-and-release that changes this behaviour. Results in a build error for me, but that's another topic. Building twice, once with -release, and once with -debug separately work flawlessly however.

                                      I used configure.bat as shown in the links, I didn't manually change the target output of the build.

                                      C 1 Reply Last reply 17 Dec 2023, 17:54
                                      1
                                      • H human99
                                        17 Dec 2023, 15:15

                                        @Christian-Ehrlicher Thanks for the extra info. I followed https://wiki.qt.io/Building_Qt_6_from_Git, and looking at the link you provided, it outlines the same steps. The only difference being that the link I followed also shows how to get the sources from the qt git repo.

                                        I don't know if it's a bug on QT's side, I suppose it would be if the build's default behaviour should be to build both debug and release libraries.

                                        To be specific: After I ran configure.bat, the batch script's output to the commandline outlines your build options:

                                        Build options:
                                        Mode ................................... release
                                        ...
                                        

                                        And in my case the mode was set to release. And as a sanity check looking in the install folder, the names of all of the .dlls and .lib files do not end with d, so they're definitely the release binary files.

                                        There is a flag you could pass to configure.bat called -debug-and-release that changes this behaviour. Results in a build error for me, but that's another topic. Building twice, once with -release, and once with -debug separately work flawlessly however.

                                        I used configure.bat as shown in the links, I didn't manually change the target output of the build.

                                        C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 17 Dec 2023, 17:54 last edited by
                                        #19

                                        @human99 Ok, thx for the investigation. I'll take a look on it - it should default to debug-and-release to avoid the problems you've encountered.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        1 Reply Last reply
                                        1

                                        4/19

                                        16 Dec 2023, 09:32

                                        15 unread
                                        • Login

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