Getting Debug Heap correction from destructor of QStringList
-
Hello, As mentioned in my first post when I tried almost similar code in console application then I am not seeing this error.
====Sample code Starts=================#include <iostream> #include <QtCore/qcoreapplication.h> #include <QtCore/qlist.h> bool DoSomeManipulation() { // Creating a QStringList QStringList fruits; // Adding items to the list fruits << "Apple" << "Banana" << "Cherry" << "Date"; // Accessing elements using index qDebug() << "First fruit:" << fruits.at(0); qDebug() << "Last fruit:" << fruits.last(); // Iterating through the list qDebug() << "\nAll fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } // Checking if list contains a specific item QString searchFruit = "Banana"; if (fruits.contains(searchFruit)) { qDebug() << "\nFound" << searchFruit << "in the list."; } else { qDebug() << "\n" << searchFruit << "not found in the list."; } // Sorting the list alphabetically fruits.sort(); qDebug() << "\nSorted fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } return true; } int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); bool t = DoSomeManipulation(); return app.exec(); }
===========Sample code ends====================================
My function is exact similar which gets called on some windows button action.
====Sample code starts=========================void COpenDlg::SaveSettingFilesList(QComboBox* cb, QString prefix) { if (cb->count() == 0) { return; } QStringList filesList; if (!cb->currentText().isEmpty()) { filesList.append(ConvertFromQtSeparator(cb->currentText())); } else { filesList.append("."); } for (int i = 0, iComboCount = cb->count(); i < iComboCount; ++i) { QString item = ConvertFromQtSeparator(cb->itemText(i)); if (!item.isEmpty() && !filesList.contains(item, Qt::CaseInsensitive)) { filesList.append(item); } } SettingsGroupInit group("FilesOpeningHistory"); group.settings.setValue(prefix + QString("Files"), filesList); if (!cb->currentText().isEmpty()) { QString combCurrentTex = cb->currentText(); group.settings.setValue(prefix + QString("Directory"), ConvertFromQtSeparator(DIRECTORY::directory(combCurrentTex))); } }
====Sample code ends==========================
While exiting this function I get crash. The call stack looks likeThe assert window
Please let me know if I can add any additional information.
-
Hello, As mentioned in my first post when I tried almost similar code in console application then I am not seeing this error.
====Sample code Starts=================#include <iostream> #include <QtCore/qcoreapplication.h> #include <QtCore/qlist.h> bool DoSomeManipulation() { // Creating a QStringList QStringList fruits; // Adding items to the list fruits << "Apple" << "Banana" << "Cherry" << "Date"; // Accessing elements using index qDebug() << "First fruit:" << fruits.at(0); qDebug() << "Last fruit:" << fruits.last(); // Iterating through the list qDebug() << "\nAll fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } // Checking if list contains a specific item QString searchFruit = "Banana"; if (fruits.contains(searchFruit)) { qDebug() << "\nFound" << searchFruit << "in the list."; } else { qDebug() << "\n" << searchFruit << "not found in the list."; } // Sorting the list alphabetically fruits.sort(); qDebug() << "\nSorted fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } return true; } int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); bool t = DoSomeManipulation(); return app.exec(); }
===========Sample code ends====================================
My function is exact similar which gets called on some windows button action.
====Sample code starts=========================void COpenDlg::SaveSettingFilesList(QComboBox* cb, QString prefix) { if (cb->count() == 0) { return; } QStringList filesList; if (!cb->currentText().isEmpty()) { filesList.append(ConvertFromQtSeparator(cb->currentText())); } else { filesList.append("."); } for (int i = 0, iComboCount = cb->count(); i < iComboCount; ++i) { QString item = ConvertFromQtSeparator(cb->itemText(i)); if (!item.isEmpty() && !filesList.contains(item, Qt::CaseInsensitive)) { filesList.append(item); } } SettingsGroupInit group("FilesOpeningHistory"); group.settings.setValue(prefix + QString("Files"), filesList); if (!cb->currentText().isEmpty()) { QString combCurrentTex = cb->currentText(); group.settings.setValue(prefix + QString("Directory"), ConvertFromQtSeparator(DIRECTORY::directory(combCurrentTex))); } }
====Sample code ends==========================
While exiting this function I get crash. The call stack looks likeThe assert window
Please let me know if I can add any additional information.
@OnkarP
You show a crash on destructing theQStringList filesList
passed to aSettingsGroupInit group
. But you don't show its code so nobody knows what it does with the parameter, which later goes out of scope. Can you not produce a simple, small, complete example exhibiting the crash? -
@JonB : I have spent some time on reproducing this issue outside my application. Here is the code change I done to reproduce the problem. I have called the same function more than once in my sample console application and I am able to reproduce the issue.
// TestQT.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <QtCore/qcoreapplication.h> #include <QtCore/qlist.h> bool DoSomeManipulation() { // Creating a QStringList QStringList fruits; // Adding items to the list fruits << "Apple" << "Banana" << "Cherry" << "Date"; // Accessing elements using index qDebug() << "First fruit:" << fruits.at(0); qDebug() << "Last fruit:" << fruits.last(); // Iterating through the list qDebug() << "\nAll fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } // Checking if list contains a specific item QString searchFruit = "Banana"; if (fruits.contains(searchFruit)) { qDebug() << "\nFound" << searchFruit << "in the list."; } else { qDebug() << "\n" << searchFruit << "not found in the list."; } // Sorting the list alphabetically fruits.sort(); qDebug() << "\nSorted fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } return true; } int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); bool t = DoSomeManipulation(); bool t1 = DoSomeManipulation(); bool t2 = DoSomeManipulation(); return app.exec(); }
My Dependencies
Additional include directories
Crash stack
My QTDIr value
Hope you will also able to see the crash!
-
@JonB : I have spent some time on reproducing this issue outside my application. Here is the code change I done to reproduce the problem. I have called the same function more than once in my sample console application and I am able to reproduce the issue.
// TestQT.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <QtCore/qcoreapplication.h> #include <QtCore/qlist.h> bool DoSomeManipulation() { // Creating a QStringList QStringList fruits; // Adding items to the list fruits << "Apple" << "Banana" << "Cherry" << "Date"; // Accessing elements using index qDebug() << "First fruit:" << fruits.at(0); qDebug() << "Last fruit:" << fruits.last(); // Iterating through the list qDebug() << "\nAll fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } // Checking if list contains a specific item QString searchFruit = "Banana"; if (fruits.contains(searchFruit)) { qDebug() << "\nFound" << searchFruit << "in the list."; } else { qDebug() << "\n" << searchFruit << "not found in the list."; } // Sorting the list alphabetically fruits.sort(); qDebug() << "\nSorted fruits:"; for (const QString& fruit : fruits) { qDebug() << fruit; } return true; } int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); bool t = DoSomeManipulation(); bool t1 = DoSomeManipulation(); bool t2 = DoSomeManipulation(); return app.exec(); }
My Dependencies
Additional include directories
Crash stack
My QTDIr value
Hope you will also able to see the crash!
@OnkarP
Your code looks good. The only thing which alters the list (apart from the initial population) is thefruits.sort()
so I would try commenting that out to see if different behaviour.You might try with code as simple as:
bool DoSomeManipulation() { // Creating a QStringList QStringList fruits; // Adding items to the list fruits << "Apple" << "Banana" << "Cherry" << "Date"; return true; }
You call it 3 times, but do not say if (a) this is required to make it go wrong or (b) which iteration it falls over on.
The "crash site" indicates that the memory allocation area is corrupted, for whatever reason. Make sure you are consistently compiling for 64-bit and the program depends on the MSVC debug runtime (something like
VCRUNTIMED.DLL
orMSVCRTD.DLL
? note the trailingD
), with no sign of a non-debug version.Other than that you will have to await someone with Windows and a similar version of MSVC/Qt to see if happens to them. If you can try a later Qt version than 6.6.3 (e.g. 6.7?) you might test that.
-
Hello,
I investigated further as per your suggestion and have below observations.
In my sample application the current runtime setting isIf I run the code as mentioned above I was getting heap crash. If I add dependency on msvcrtd.lib in my project properties then
the crash is not coming in the same code.Is there anything problem with Qt libraries or these settings are compulsory for Qt6? Because for Qt5 I have not added any such settings when using prebuilt libraries of 32 bit.
Can you please check whether if I am missing anything on this?
In my project if I add dependencies "msvcrtd.lib VCRUNTIMED.lib" then I am able to fix the problem of crashing. But further I am observing specific APIs are not behaving as it was behaving with Qt5.
QtConcurrent::blockingMap(_parts, Movepart(position));
Do you have any suggestions for this ?
Thanks in advance for help!
Regards,
OnkarPS : I have checked with QT 6.7.1 and saw similar issue there.
-
Use a proper build system generator like CMake, qmake or the Qt Visual Studio plugin (which uses CMake) to avoid such problems.
-
The above mentioned code where I need to add explicit dependency on msvcrtd.lib is created using new QT project in Visual studio using QT add-on.
The workaround which is working for me when runtime library is Multi-threaded Debug (/MTd) is adding msvcrtd.lib in additional dependencies. If I don't add this then I was getting exception for code which I shared earlier.
-
1>LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
In addition to above there is also a warning while building this project. I am looking for confirmation whether there is some issue in my project settings which I missed and which is causing these problems or extra steps to work with QT6MSVC2019_64.