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 crash on Mac because of db
Forum Updated to NodeBB v4.3 + New Features

Qt crash on Mac because of db

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.2k 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.
  • M madaminov0888

    Hello, I am creating simple Qt project with database, I have used QSql but when I tried to get data from db, my app crashes and gives "Problem report, QtApp quit unexpectedly". And I thought it was from Sql but I used just txt, bin and dat binary files in c++ and nothing changed, I have changed settings and give access Qt creator and my app to all hard disk folders, but nothing changed. What is problem?
    My computer runs on macOS 13, M1 Pro chip
    #crash, #macOS, #mac

    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @madaminov0888 said in Qt crash on Mac because of db:

    What is problem?

    re-read your question and ask yourself how we should help with such a description.

    Please provide some code and/or use a debugger to see why it crashes.

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

    M 1 Reply Last reply
    1
    • M madaminov0888

      Hello, I am creating simple Qt project with database, I have used QSql but when I tried to get data from db, my app crashes and gives "Problem report, QtApp quit unexpectedly". And I thought it was from Sql but I used just txt, bin and dat binary files in c++ and nothing changed, I have changed settings and give access Qt creator and my app to all hard disk folders, but nothing changed. What is problem?
      My computer runs on macOS 13, M1 Pro chip
      #crash, #macOS, #mac

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @madaminov0888 said in Qt crash on Mac because of db:

      And I thought it was from Sql but I used just txt, bin and dat binary files in c++ and nothing changed

      Then create a minimal, 4-line Qt program without any database and see if that crashes.

      M 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @madaminov0888 said in Qt crash on Mac because of db:

        What is problem?

        re-read your question and ask yourself how we should help with such a description.

        Please provide some code and/or use a debugger to see why it crashes.

        M Offline
        M Offline
        madaminov0888
        wrote on last edited by
        #4

        @Christian-Ehrlicher that is the problem I don't know either, why is crashing in the terminal print only "QtApp crashed", when I tried to read data from db, it is simple phone book app that get data from user and saves in db,

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QListWidgetItem>
        #include <QString>
        #include "myitem.h"
        #include <fstream>
        #include "user.h"
        #include <iostream>

        using namespace std;

        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        {

        User u;
        fstream file;
        string filename = "/Users/muhammadjonmadaminov/Desktop/QtProjects/Calculator/data.bin";
        file.open(filename, ios::binary | ios::in | ios::out | ios::app);
        if (!file.is_open()) {
            // if the file does not exist, create it
            file.open(filename, ios::binary | ios::out);
            if (!file.is_open()) {
                cout << "Error creating file" << endl;
            }
            file.close();
            // try to open the file again in append mode
            file.open(filename, ios::binary | ios::in | ios::out | ios::app);
            if (!file.is_open()) {
                cout << "Error opening file" << endl;
            }
        }
        
        
        ui->setupUi(this);
        ui->tabWidget->tabBar()->hide();
        ui->tabWidget->setCurrentWidget(ui->tab);
        file.seekg(0);
        while(file.read((char*)&u,sizeof(u))){
            QString name = u.getName();
            //QString surname = u.getSurname();
            QString number = u.getNumber();
            QListWidgetItem *item = new QListWidgetItem;
            MyItem *myitem = new MyItem(name, number);
            ui->listWidget->addItem(item);
            item->setSizeHint(QSize(0, 70));
            ui->listWidget->setItemWidget(item, myitem);
        }
        file.close();
        

        }
        this is the code.

        1 Reply Last reply
        0
        • JonBJ JonB

          @madaminov0888 said in Qt crash on Mac because of db:

          And I thought it was from Sql but I used just txt, bin and dat binary files in c++ and nothing changed

          Then create a minimal, 4-line Qt program without any database and see if that crashes.

          M Offline
          M Offline
          madaminov0888
          wrote on last edited by
          #5

          @JonB no it would not crash because the reason of crash is opening file or Sql db, there is not error while debugging and running application, crash occurs when I run code and app opens and when I input data and push button save or get from db, app just quits. My description can't give more info about crash but I don't know either why is this happening.

          Christian EhrlicherC 1 Reply Last reply
          0
          • M madaminov0888

            @JonB no it would not crash because the reason of crash is opening file or Sql db, there is not error while debugging and running application, crash occurs when I run code and app opens and when I input data and push button save or get from db, app just quits. My description can't give more info about crash but I don't know either why is this happening.

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

            Apart from the fact that I don't see any sql code or database here - use a debugger and see where it crashes. That's the live of a developer.

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

            M 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              Apart from the fact that I don't see any sql code or database here - use a debugger and see where it crashes. That's the live of a developer.

              M Offline
              M Offline
              madaminov0888
              wrote on last edited by
              #7

              @Christian-Ehrlic I said I used Sql and it crashed because of that I tried using binary files and even just .txt files, and nothing changed, I don't think it is from Qt or debug, I think there is some error with macOS 13, in previous 12 or 11 versions it worked I reported in macOS care, and waiting for answer, I did not find anything but thank you for your answers

              Christian EhrlicherC 1 Reply Last reply
              0
              • M madaminov0888

                @Christian-Ehrlic I said I used Sql and it crashed because of that I tried using binary files and even just .txt files, and nothing changed, I don't think it is from Qt or debug, I think there is some error with macOS 13, in previous 12 or 11 versions it worked I reported in macOS care, and waiting for answer, I did not find anything but thank you for your answers

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

                @madaminov0888 said in Qt crash on Mac because of db:

                , and waiting for answer,

                Just a reminder: A program you created is crashing on your computer and you are waiting that it goes away magically?

                I don't see how we can help here - esp. since you want something from us but don't provide anything except some code pieces which don't compile/aren't complete and no hint where it crashes.

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

                M 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @madaminov0888 said in Qt crash on Mac because of db:

                  , and waiting for answer,

                  Just a reminder: A program you created is crashing on your computer and you are waiting that it goes away magically?

                  I don't see how we can help here - esp. since you want something from us but don't provide anything except some code pieces which don't compile/aren't complete and no hint where it crashes.

                  M Offline
                  M Offline
                  madaminov0888
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher ok ok, there was my mistake in settings accesebilty wasn't changed to application it was changed to Qt creator and because of that my Mac did not give me access to use file or db. And after that App did not Quit quickly, I debugged and get this error:

                  Calculator(7947,0x1f954db40) malloc: *** error for object 0x600003ee81e0: pointer being freed was not allocated
                  Calculator(7947,0x1f954db40) malloc: *** set a breakpoint in malloc_error_break to debug
                  Detected Qt Version: 0x60500 (namespace='no namespace')
                  Found function QObject::property: 0x101db2b24
                  Found function QObject::customEvent: 0x101daaaf4
                  

                  this was my code:

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QListWidgetItem>
                  #include <QString>
                  #include "myitem.h"
                  #include <fstream>
                  #include "user.h"
                  #include <iostream>
                  #include <QSqlError>
                  #include <iostream>
                  
                  
                  
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                  
                      User u;
                      fstream file;
                      //string filename = "/Users/muhammadjonmadaminov/Desktop/QtProjects/Calculator/date.txt";
                      db = QSqlDatabase::addDatabase("QSQLITE");
                      db.setDatabaseName("./data.db");
                      if(db.open()){
                          cout << "Data opened " << endl;
                      }
                      else{
                          qDebug() << db.lastError().databaseText();
                      }
                      query = new QSqlQuery(db);
                      query->prepare("insert into TelephoneBook (Name, Surname, Number)");
                      //query->exec("CREATE TelephoneBook(Name TEXT, Surname text, Number TEXT);");
                  
                      ui->setupUi(this);
                      ui->tabWidget->tabBar()->hide();
                      ui->tabWidget->setCurrentWidget(ui->tab);
                      file.seekg(0);
                      while(file.read((char*)&u,sizeof(u))){
                          QString name = u.getName();
                          //QString surname = u.getSurname();
                          QString number = u.getNumber();
                          QListWidgetItem *item = new QListWidgetItem;
                          MyItem *myitem = new MyItem(name, number);
                          ui->listWidget->addItem(item);
                          item->setSizeHint(QSize(0, 70));
                          ui->listWidget->setItemWidget(item, myitem);
                      }
                      file.close();
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  
                  void MainWindow::on_pushButton_clicked()
                  {
                      ui->lineEdit_4->clear();
                      ui->lineEdit_5->clear();
                      ui->lineEdit_6->clear();
                      ui->tabWidget->setCurrentWidget(ui->tab_2);
                  }
                  
                  
                  void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
                  {
                      ui->tabWidget->setCurrentWidget(ui->tab);
                      fstream file;
                      User u;
                      QString tx;
                      QString number;
                      QString surname;
                      string filename = "/Users/muhammadjonmadaminov/Desktop/QtProjects/Calculator/date.txt";
                      file.open(filename, ios::binary | ios::in | ios::out | ios::app);
                      if (!file.is_open()) {
                          // if the file does not exist, create it
                          file.open(filename, ios::binary | ios::out);
                          if (!file.is_open()) {
                              cout << "Error creating file" << endl;
                          }
                          file.close();
                          // try to open the file again in append mode
                          file.open(filename, ios::binary | ios::in | ios::out | ios::app);
                          if (!file.is_open()) {
                              cout << "Error opening file" << endl;
                          }
                      }
                      if (dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))) {
                          tx = dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))->text();
                          number = dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))->text_2();
                      }
                      else {
                          tx = item->text();
                      }
                      file.seekg(0);
                      while(file.read((char*)&u, sizeof(u))){
                          if(u.getName() == tx and u.getNumber() == number){
                              surname = u.getSurname();
                          }
                      }
                      ui->lineEdit->setText(tx);
                      ui->lineEdit_2->setText(surname);
                      ui->lineEdit_3->setText(number);
                  
                  }
                  
                  
                  void MainWindow::on_pushButton_2_clicked()
                  {
                      db = QSqlDatabase::addDatabase("QSQLITE");
                      db.setDatabaseName("./data.db");
                      if(db.open()){
                          cout << "Data opened " << endl;
                      }
                      else{
                          qDebug() << db.lastError().databaseText();
                      }
                      query = new QSqlQuery(db);
                  
                      User u;
                      QListWidgetItem * item =new QListWidgetItem;
                      QString name = ui->lineEdit_4->text();
                      QString surname = ui->lineEdit_5->text();
                      QString number = ui->lineEdit_6->text();
                      u.setData(name, surname, number);
                      query->prepare("insert into TelephoneBook(Name, Surname,Number) values ('" + name + "', '" + surname + "', '" + number + "')");
                      if(query->exec()){
                          cout << "All done" << endl;
                          db.close();
                      }
                      MyItem *myitem = new MyItem(name, number);
                      ui->listWidget->addItem(item);
                      item->setSizeHint(QSize(0,70));
                      ui->listWidget->setItemWidget(item, myitem);
                      ui->tabWidget->setCurrentWidget(ui->tab);
                  }
                  

                  and in terminal returned this error:

                  Data opened 
                  QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
                  QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
                  Data opened 
                  
                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • M madaminov0888

                    @Christian-Ehrlicher ok ok, there was my mistake in settings accesebilty wasn't changed to application it was changed to Qt creator and because of that my Mac did not give me access to use file or db. And after that App did not Quit quickly, I debugged and get this error:

                    Calculator(7947,0x1f954db40) malloc: *** error for object 0x600003ee81e0: pointer being freed was not allocated
                    Calculator(7947,0x1f954db40) malloc: *** set a breakpoint in malloc_error_break to debug
                    Detected Qt Version: 0x60500 (namespace='no namespace')
                    Found function QObject::property: 0x101db2b24
                    Found function QObject::customEvent: 0x101daaaf4
                    

                    this was my code:

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include <QListWidgetItem>
                    #include <QString>
                    #include "myitem.h"
                    #include <fstream>
                    #include "user.h"
                    #include <iostream>
                    #include <QSqlError>
                    #include <iostream>
                    
                    
                    
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                    
                        User u;
                        fstream file;
                        //string filename = "/Users/muhammadjonmadaminov/Desktop/QtProjects/Calculator/date.txt";
                        db = QSqlDatabase::addDatabase("QSQLITE");
                        db.setDatabaseName("./data.db");
                        if(db.open()){
                            cout << "Data opened " << endl;
                        }
                        else{
                            qDebug() << db.lastError().databaseText();
                        }
                        query = new QSqlQuery(db);
                        query->prepare("insert into TelephoneBook (Name, Surname, Number)");
                        //query->exec("CREATE TelephoneBook(Name TEXT, Surname text, Number TEXT);");
                    
                        ui->setupUi(this);
                        ui->tabWidget->tabBar()->hide();
                        ui->tabWidget->setCurrentWidget(ui->tab);
                        file.seekg(0);
                        while(file.read((char*)&u,sizeof(u))){
                            QString name = u.getName();
                            //QString surname = u.getSurname();
                            QString number = u.getNumber();
                            QListWidgetItem *item = new QListWidgetItem;
                            MyItem *myitem = new MyItem(name, number);
                            ui->listWidget->addItem(item);
                            item->setSizeHint(QSize(0, 70));
                            ui->listWidget->setItemWidget(item, myitem);
                        }
                        file.close();
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    
                    void MainWindow::on_pushButton_clicked()
                    {
                        ui->lineEdit_4->clear();
                        ui->lineEdit_5->clear();
                        ui->lineEdit_6->clear();
                        ui->tabWidget->setCurrentWidget(ui->tab_2);
                    }
                    
                    
                    void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
                    {
                        ui->tabWidget->setCurrentWidget(ui->tab);
                        fstream file;
                        User u;
                        QString tx;
                        QString number;
                        QString surname;
                        string filename = "/Users/muhammadjonmadaminov/Desktop/QtProjects/Calculator/date.txt";
                        file.open(filename, ios::binary | ios::in | ios::out | ios::app);
                        if (!file.is_open()) {
                            // if the file does not exist, create it
                            file.open(filename, ios::binary | ios::out);
                            if (!file.is_open()) {
                                cout << "Error creating file" << endl;
                            }
                            file.close();
                            // try to open the file again in append mode
                            file.open(filename, ios::binary | ios::in | ios::out | ios::app);
                            if (!file.is_open()) {
                                cout << "Error opening file" << endl;
                            }
                        }
                        if (dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))) {
                            tx = dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))->text();
                            number = dynamic_cast<MyItem*>(ui->listWidget->itemWidget(item))->text_2();
                        }
                        else {
                            tx = item->text();
                        }
                        file.seekg(0);
                        while(file.read((char*)&u, sizeof(u))){
                            if(u.getName() == tx and u.getNumber() == number){
                                surname = u.getSurname();
                            }
                        }
                        ui->lineEdit->setText(tx);
                        ui->lineEdit_2->setText(surname);
                        ui->lineEdit_3->setText(number);
                    
                    }
                    
                    
                    void MainWindow::on_pushButton_2_clicked()
                    {
                        db = QSqlDatabase::addDatabase("QSQLITE");
                        db.setDatabaseName("./data.db");
                        if(db.open()){
                            cout << "Data opened " << endl;
                        }
                        else{
                            qDebug() << db.lastError().databaseText();
                        }
                        query = new QSqlQuery(db);
                    
                        User u;
                        QListWidgetItem * item =new QListWidgetItem;
                        QString name = ui->lineEdit_4->text();
                        QString surname = ui->lineEdit_5->text();
                        QString number = ui->lineEdit_6->text();
                        u.setData(name, surname, number);
                        query->prepare("insert into TelephoneBook(Name, Surname,Number) values ('" + name + "', '" + surname + "', '" + number + "')");
                        if(query->exec()){
                            cout << "All done" << endl;
                            db.close();
                        }
                        MyItem *myitem = new MyItem(name, number);
                        ui->listWidget->addItem(item);
                        item->setSizeHint(QSize(0,70));
                        ui->listWidget->setItemWidget(item, myitem);
                        ui->tabWidget->setCurrentWidget(ui->tab);
                    }
                    

                    and in terminal returned this error:

                    Data opened 
                    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
                    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
                    Data opened 
                    
                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    You must not open a database twice. Please follow the documentation on how to open and use a QSqlDatabase: https://doc.qt.io/qt-6/qsqldatabase.html#details

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

                    M 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      You must not open a database twice. Please follow the documentation on how to open and use a QSqlDatabase: https://doc.qt.io/qt-6/qsqldatabase.html#details

                      M Offline
                      M Offline
                      madaminov0888
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher yeah I think I need to sleep, thank you for your help, I thought It would be easy creating simple project for University

                      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