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. QString causing Memory Error
Forum Updated to NodeBB v4.3 + New Features

QString causing Memory Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.1k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    Pawx
    wrote on last edited by Pawx
    #1

    Hey everyone,

    a friend of mine and me have started to mess around with c++ and QT6. We were able to create what we desired until we encountered the QString class. We somehow cant get it to work and we always receive an error code related to some memory issues (0xC0000374) and (0xC0000005). To debug this, we copied a simple example into our main.cpp and we still get (0xC0000005) running this code.

    #include <QString>
    
    int main(int argc, char *argv[]) {
    
        /*
        QApplication app(argc, argv);
    
        MainWindow window;
        window.show();
    
        return app.exec();
         */
        QString str = "Meal";
        str.insert(1, QString("ontr"));
    }
    

    If i saw it correctly, the issue occurs when the insert() is called. My debugger led me to the qstring.h file where the destructor is highlighted. The next step is in the qarraydatapointer.h where the call to free(d) is highlighted within the destructor here:

    ~QArrayDataPointer()
        {
            if (!deref()) {
                (*this)->destroyAll();
                free(d);
            }
        }
    

    We are using CMake to build our solution and our CMakeLists.txt looks like this, if thats important:

    cmake_minimum_required(VERSION 3.26)
    project(Chess)
    
    ###QT
    ###Set Qt6_DIR variable
    set(Qt6_DIR $ENV{QT6_CMAKE_DIR})
    
    ###Find the QtWidgets library
    find_package(Qt6 COMPONENTS Widgets REQUIRED)
    
    ###Add the include directories for the Qt 6 Widgets module to
    ### the compile lines.
    include_directories(${Qt6Widgets_INCLUDE_DIRS})
    
    ###Use the compile definitions defined in the Qt 6 Widgets module
    add_definitions(${Qt6Widgets_DEFINITIONS})
    
    ###Add compiler flags for building executables (-fPIE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt6Widgets_EXECUTABLE_COMPILE_FLAGS}")
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
    
    ###QT END
    
    set(CMAKE_CXX_STANDARD 23)
    
    add_executable(Chess main.cpp MainWindow.cpp GridCell.cpp resources.qrc)
    
    
    ###QT LINK
    ###Link against Qt libraries
    target_link_libraries(Chess Qt6::Widgets)
    

    Some of the CMakeLists.txt is made with the help of ChatGPT and im not 100% sure if this is correct.

    And some additional info:
    What we want to do in the first place was to adjust the value of an QString based of a an switch case statement like this:

    void GridCell::SetSprite(ChessClass chessClass)
    {
        QString value;
    
        switch(chessClass)
        {
            case Rook:
                value = m_team ? "RookBlack" : "RookWhite";
                break;
            case Knight:
                value = m_team ? "KnightBlack" : "KnightWhite";
                break;
            case King:
                value = m_team ? "KingBlack" : "KingWhite";
                break;
            case Queen:
                value = m_team ? "QueenBlack" : "QueenWhite";
                break;
            case Pawn:
                value = m_team ? "PawnBlack" : "PawnWhite";
                break;
            case Bishop:
                value = m_team ? "BishopBlack" : "BishopWhite";
                break;
            case None:
                break;
        }
    
        setStyleSheet("background-color: rgb(252, 149, 149); border: 2px solid black; background-image: url(:/images/"+ value +".png); background-repeat: repeat-y-x; background-position: center center;");
    }
    

    Any help would be great!

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

      Hi and welcome to devnet,

      On which OS are you getting this issue ?

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

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        On which OS are you getting this issue ?

        P Offline
        P Offline
        Pawx
        wrote on last edited by
        #3

        @SGaist Hey,

        thanks for your quick reply. We are using Windows 10 and Windows 11, both 64 bit.

        Christian EhrlicherC 1 Reply Last reply
        0
        • P Pawx

          @SGaist Hey,

          thanks for your quick reply. We are using Windows 10 and Windows 11, both 64 bit.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          You are mixing debug and release dlls. How do you build / link your app? Please use cmake or qmake.

          /edit: What should this line do/ why is it needed:

          set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt6Widgets_EXECUTABLE_COMPILE_FLAGS}")

          The include and add_definitions call is also not needed since you link against the imported QtWidgets target.

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

          C 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            You are mixing debug and release dlls. How do you build / link your app? Please use cmake or qmake.

            /edit: What should this line do/ why is it needed:

            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt6Widgets_EXECUTABLE_COMPILE_FLAGS}")

            The include and add_definitions call is also not needed since you link against the imported QtWidgets target.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @Christian-Ehrlicher For my education, how do you determine this? Bitter experience? Is there something in the original information that I am missing?

            Christian EhrlicherC 1 Reply Last reply
            0
            • C ChrisW67

              @Christian-Ehrlicher For my education, how do you determine this? Bitter experience? Is there something in the original information that I am missing?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ChrisW67 said in QString causing Memory Error:

              Bitter experience? Is there something in the original information that I am missing?

              This looks like something this way - the memory is allocated in main.cpp - therefore the e.g. msvc debug allocator is used but deleted within the Qt dll so the release (de)allocator is used which causes such problems. Yes I've seen such problems more than once when mixing debug and release libs.

              The op should take a look with e.g. dependency walker or 'dependencies gui' (better) to not mix debug and release libs.

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

              P 1 Reply Last reply
              4
              • Christian EhrlicherC Christian Ehrlicher

                @ChrisW67 said in QString causing Memory Error:

                Bitter experience? Is there something in the original information that I am missing?

                This looks like something this way - the memory is allocated in main.cpp - therefore the e.g. msvc debug allocator is used but deleted within the Qt dll so the release (de)allocator is used which causes such problems. Yes I've seen such problems more than once when mixing debug and release libs.

                The op should take a look with e.g. dependency walker or 'dependencies gui' (better) to not mix debug and release libs.

                P Offline
                P Offline
                Pawx
                wrote on last edited by Pawx
                #7

                @Christian-Ehrlicher

                How do i check the debug/release versions? I downloaded the dependencies gui and opened my executable in it.

                It gave me an output like this:
                3c01765e-7170-413d-a358-06f60d362a47-image.png

                We are only using the QTBase Repository and compiled it ourselfs, if that information helps.
                My mate actually got the code to work by installing qt via the installer, but i wanted to look further into this to see if we are able to fix it.

                Christian EhrlicherC 1 Reply Last reply
                0
                • P Pawx

                  @Christian-Ehrlicher

                  How do i check the debug/release versions? I downloaded the dependencies gui and opened my executable in it.

                  It gave me an output like this:
                  3c01765e-7170-413d-a358-06f60d362a47-image.png

                  We are only using the QTBase Repository and compiled it ourselfs, if that information helps.
                  My mate actually got the code to work by installing qt via the installer, but i wanted to look further into this to see if we are able to fix it.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pawx Since you're using MinGW which you did not told us, my assumption can not be correct.
                  What I see is that you're using another MinGW version than the one provided by Qt - make sure to use the MinGW compiler and libs (libstdc++6.dll/libgcc_s_seh-1.dll) provided by Qt.

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

                  P 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @Pawx Since you're using MinGW which you did not told us, my assumption can not be correct.
                    What I see is that you're using another MinGW version than the one provided by Qt - make sure to use the MinGW compiler and libs (libstdc++6.dll/libgcc_s_seh-1.dll) provided by Qt.

                    P Offline
                    P Offline
                    Pawx
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher

                    How do i make sure to use the correct MinGW version? Is this something i can configure in my CMakeList or where can i adjust this?

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • P Pawx

                      @Christian-Ehrlicher

                      How do i make sure to use the correct MinGW version? Is this something i can configure in my CMakeList or where can i adjust this?

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Pawx said in QString causing Memory Error:

                      How do i make sure to use the correct MinGW version?

                      Use the correct kit settings and set the PATH correct.

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

                      P 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @Pawx said in QString causing Memory Error:

                        How do i make sure to use the correct MinGW version?

                        Use the correct kit settings and set the PATH correct.

                        P Offline
                        P Offline
                        Pawx
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher

                        Is there a way to configure the kits without qt creator? Currently i dont have QTCreator installed and just compiled the repository and built it. Is QTCreator mandatory/recommended to use?

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • P Pawx

                          @Christian-Ehrlicher

                          Is there a way to configure the kits without qt creator? Currently i dont have QTCreator installed and just compiled the repository and built it. Is QTCreator mandatory/recommended to use?

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Pawx said in QString causing Memory Error:

                          QTCreator installed and just compiled the repository and built it

                          So you compiled Qt by yourself?
                          What MinGW version do you use?

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

                          P 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @Pawx said in QString causing Memory Error:

                            QTCreator installed and just compiled the repository and built it

                            So you compiled Qt by yourself?
                            What MinGW version do you use?

                            P Offline
                            P Offline
                            Pawx
                            wrote on last edited by Pawx
                            #13

                            @Christian-Ehrlicher

                            Yes, i compiled it myself.
                            I got my mingw from winlabs, and its version file says:

                            winlibs personal build version gcc-13.2.0-llvm-17.0.6-mingw-w64ucrt-11.0.1-r5
                            
                            This is the winlibs Intel/AMD 64-bit standalone build of:
                            - GCC 13.2.0
                            - GDB 14.1
                            - LLVM/Clang/LLD/LLDB 17.0.6
                            - MinGW-w64 11.0.1 (linked with ucrt)
                            - GNU Binutils 2.42
                            - GNU Make 4.4.1
                            - PExports 0.47
                            - dos2unix 7.5.2
                            - Yasm 1.3.0
                            - NASM 2.16.01
                            - JWasm 2.12pre
                            - ccache version 4.9
                            - CMake 3.28.2
                            - ninja 1.11.1.git.kitware.jobserver-1
                            - Doxygen 1.10.0
                            
                            Thread model: posix
                            Runtime library: UCRT (Windows 10 or higher, or when [Update for Universal C Runtime](https://support.microsoft.com/en-us/topic/update-for-universal-c-runtime-in-windows-c0514201-7fe6-95a3-b0a5-287930f3560c) is installed on older Windows versions, not supported on systems older than Windows 7 SP1 and Windows Server 2008 R2 SP1)
                            
                            This build was compiled with GCC 13.2.0 and packaged on 2024-02-03.
                            
                            Please check out http://winlibs.com/ for the latest personal build.
                            
                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • P Pawx

                              @Christian-Ehrlicher

                              Yes, i compiled it myself.
                              I got my mingw from winlabs, and its version file says:

                              winlibs personal build version gcc-13.2.0-llvm-17.0.6-mingw-w64ucrt-11.0.1-r5
                              
                              This is the winlibs Intel/AMD 64-bit standalone build of:
                              - GCC 13.2.0
                              - GDB 14.1
                              - LLVM/Clang/LLD/LLDB 17.0.6
                              - MinGW-w64 11.0.1 (linked with ucrt)
                              - GNU Binutils 2.42
                              - GNU Make 4.4.1
                              - PExports 0.47
                              - dos2unix 7.5.2
                              - Yasm 1.3.0
                              - NASM 2.16.01
                              - JWasm 2.12pre
                              - ccache version 4.9
                              - CMake 3.28.2
                              - ninja 1.11.1.git.kitware.jobserver-1
                              - Doxygen 1.10.0
                              
                              Thread model: posix
                              Runtime library: UCRT (Windows 10 or higher, or when [Update for Universal C Runtime](https://support.microsoft.com/en-us/topic/update-for-universal-c-runtime-in-windows-c0514201-7fe6-95a3-b0a5-287930f3560c) is installed on older Windows versions, not supported on systems older than Windows 7 SP1 and Windows Server 2008 R2 SP1)
                              
                              This build was compiled with GCC 13.2.0 and packaged on 2024-02-03.
                              
                              Please check out http://winlibs.com/ for the latest personal build.
                              
                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              I would try with the pre-compiled Qt libs + MinGW from Qt first.

                              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
                              0

                              • Login

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