QString causing Memory Error
-
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!
-
Hi and welcome to devnet,
On which OS are you getting this issue ?
-
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.
-
@Christian-Ehrlicher For my education, how do you determine this? Bitter experience? Is there something in the original information that I am missing?
-
@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.
-
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:
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. -
@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. -
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?
-
@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.
-
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?
-
@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? -
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.
-
I would try with the pre-compiled Qt libs + MinGW from Qt first.