Some error occuring on destructor
-
Hello!
I just refactored some code in the constructor and now the destructor seems to have some problems (see the picture).
Any ideas?
Here is my code :
#include "pickParticipants.h" #include <QtSql/qsqldatabase.h> #include <QtSql/qsqlquery.h> #include <qtablewidget.h> #include <QtCore> pickParticipants::pickParticipants(QWidget *parent) : QDialog(parent) { ui.setupUi(this); connect(ui.deletePushButton, SIGNAL(clicked()), this, SLOT(deletePilot())); connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(addNewPilot_Button())); connect(ui.okPushButton, SIGNAL(clicked()), this, SLOT(openCircuits())); if (QSqlDatabase::database("dataB").open()) { //SQL Query that fetches all the pilots along with their team and car specs QString selectAllPilots("SELECT Piloti.nume, Piloti.prenume, Echipe.nume, Masini.Serie_Sasiu, Masini.numar_masina, Masini.putere_motor, Masini.capacitate_motor FROM Piloti JOIN Echipe ON Piloti.ID_Echipa = Echipe.ID_Echipa JOIN Masini ON Piloti.ID_Pilot = Masini.ID_Pilot;"); //Execute "selectAllPilots" query against "dataB" database QSqlQuery qry(selectAllPilots, QSqlDatabase::database("dataB")); //Count the number of records int rowCounter = 0; //Build drivers table while (qry.next()) { ui.driversTable->insertRow(rowCounter); ui.driversTable->setItem(rowCounter, 0, new QTableWidgetItem(qry.value(0).toString() + " " + qry.value(1).toString())); for (int colCounter = 1; colCounter < ui.driversTable->columnCount(); colCounter++) { ui.driversTable->setItem(rowCounter, colCounter, new QTableWidgetItem(qry.value(colCounter + 1).toString())); } rowCounter++; } } } pickParticipants::~pickParticipants() { QSqlDatabase::database("dataB").close(); } void pickParticipants::addNewPilot_Button() { //Set up the window addNewPilot addPilotWindow; addPilotWindow.setModal(true); //Show the window addPilotWindow.show(); } void pickParticipants::deletePilot() { } void pickParticipants::openCircuits() { //moving to the next screen "resultPage" //hide the "Pick Participants" UI this->hide(); //set the new page "PickParticipants" resultPage resultPageUI; resultPageUI.setModal(true); //show the new page resultPageUI.exec(); }
Thank you!
PS: Before refactoring the code it was working without problems
-
Hi
Do we know what type of exception or any text from it ?
I suspect the database is no longer valid, or has open queries. -
@mrjj Thank you for your quick reply!
Severity Code Description Project File Line Suppression State Error (active) E2512 the argument to a feature-test macro must be a simple identifier F1_System C:\Qt\Qt5.11.2\5.11.2\msvc2017_64\include\QtCore\qcompilerdetection.h 1327
This is the error shown in the error list.
Why would a query still be open? It didn't while I was working with the old code.
LE: It occurs when I close that window.
-
@Ovidiu_GCO
Hi
Ehh that seems completely unrelated to the code, i must say.
if you //QSqlDatabase::database("dataB").close();
does it still happen ? -
@Ovidiu_GCO
So its not the code but something else. (it seems)
Any chance pickParticipants is deleted twice ?
Also, did you try a complete clean build ?Also, did you update VS lately, as it does sound like
https://forum.qt.io/topic/93711/problem-with-new-visual-studio-update-2017-version-15-8-0-and-qt-5-9-5 -
@mrjj pickParticipants deleted twice? I don't call on the destructor anywhere else, if this is what you are asking.
Nope, didn't update VS since installation.
I just remembered something, last time it was working fine, I tried to open another window(inside this app, using a button) and it kinda flick a few times(and it didn't open at anytime). Do you think that could cause some memory damage or something like that?
-
@Ovidiu_GCO
Yes something like manual deletion. But sounds not like it.
Could you try a fresh project ?
Im not sure if something in VS got damaged. the error seems very odd. -
@Ovidiu_GCO said in Some error occuring on destructor:
void pickParticipants::addNewPilot_Button() {
//Set up the window
addNewPilot addPilotWindow;
addPilotWindow.setModal(true);
//Show the window
addPilotWindow.show();}
This is not going to work as addPilotWindow is going out of scope and is deleted when addNewPilot_Button finishes. You should either allocate it on the heap or use exec().
Also you should check the stack trace when you get that exception to see what happens at that time. -
Hi,
When the
addNewPilot_Button
function ends which is exactly afteraddPilotWindow.show()
is called. -
@Ovidiu_GCO Why don't you simply use exec() instead of show()?
-
@jsulm I changed it to
.exec()
. I don't know why I used.show()
this time, all over my code I used.exec()
. Thank you!@SGaist I get it now, but I would use that window to insert a pilot's information into the database, so it wouldn't bother me.
I didn't manage to clear that error using any of the ideas above, but I still have to check the stack trace if you could tell me how.
Thank you!
-
I don't know if this helps, but I got this in the Output Window:
Debug Assertion Failed! Program: ...l III\BD\Proiect\Qt_Project\F1_System\x64\Debug\F1_System.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 908 Expression: is_block_type_valid(header->_block_use) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) The thread 0x370c has exited with code 0 (0x0). The thread 0x1d74 has exited with code 0 (0x0). The thread 0x36c8 has exited with code 0 (0x0).
-
@Ovidiu_GCO
I don't know what is actually wrong in your code. However the stack trace tells us, not surprisingly, that it has overwritten dynamically allocated memory incorrectly, or possibly is trying to free a block which is not allocated. I presume this could occur on memory allocated/freed via C++new
/delete
, as well as Cmalloc
/free
etc., though not certain whether the C++ have their own memory allocation catchers. Don't know if you can use this to help track down your problem.... -
@JonB You are right, something is wrong with memory allocation.
I just tried to comment some of my code and it seems that the problem is located inside this
while loop
:while (qry.next()) { ui.driversTable->insertRow(rowCounter); ui.driversTable->setItem(rowCounter, 0, new QTableWidgetItem(qry.value(0).toString() + " " + qry.value(1).toString())); //we use "ui.driversTable->columnCount() - 1" because the last column is a checkbox for (int colCounter = 1; colCounter < ui.driversTable->columnCount() - 1 ; colCounter++) { ui.driversTable->setItem(rowCounter, colCounter, new QTableWidgetItem(qry.value(colCounter + 1).toString())); } rowCounter++; }
I suspect the
setItem()
lines, but I read in the documentation that the QTableWidget takes ownership of these items, so it shouldn't be a problem.Thank you all for your help and time!
-
Unrelated to the problem at hand but why not use a QTableView / QSqlTableModel or QSqlQueryModel combo ?
-
Well... You are already using Qt's Model/View since you are using a QTableWidget.
From the QSqlQueryModel documentation:
QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("SELECT name, salary FROM employee"); model->setHeaderData(0, Qt::Horizontal, tr("Name")); model->setHeaderData(1, Qt::Horizontal, tr("Salary")); QTableView *view = new QTableView; view->setModel(model); view->show();
Adapt that to the query you are using and you should be good to go.