0

Inside my small application, I'm using this database frequently to put data. It's working fine but sometime segmentation fault at line db.open() or sometime db.close().

I'm getting free() unsorted chunks:some adress. please any one help.Thanks.

  QSqlDatabase db;
    if (QSqlDatabase::contains())
    {
      db = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false);
    }
    else
    {
       db = QSqlDatabase::addDatabase( "QSQLITE" );
    }
    db.setDatabaseName( "Database/database.db" );
    QSqlQuery qry;
    if(!db.open())
    {
//          qDebug()<< db.lastError();
//          qFatal("Failed to connect.");
        cout<<"database file path problem";
    }
    if(check == "y")
    {


        qry.prepare(  "INSERT INTO serl_num (SERIAL,RESULT)" "VALUES (?,?)");
        qry.addBindValue(_serialNo);
        qry.addBindValue("Abnormal missing file");
    } 
    if(check == "n")
    {

        qry.prepare("select * from serl_num where serial=?");
        qry.addBindValue(_serialNo);
        bool ok=qry.exec();
        if(ok && qry.first())
        {
            qDebug( "serial_number is already in database" );
        }
        else
        {
            QStringList boardNamefile = QString::fromStdString(_boardFile).split('/');
            QStringList boardLayerName = QString::fromStdString(layer).split('.');
            qry.prepare(  "INSERT INTO serl_num (BOARD_NAME,LAYER_NAME,SERIAL,STATUS)" "VALUES (?,?,?,?)");
            qry.addBindValue(boardNamefile.last());
            qry.addBindValue(boardLayerName.first());
            qry.addBindValue(_serialNo);
            qry.addBindValue(0);
        }

    } 


    if( !qry.exec())
    {
        QMessageBox messageBox;
        messageBox.critical(0,"Warning","Database doesn't exist or Database file may be corrupted");
        messageBox.setFixedSize(500,200);
    }
    if(check == "y")
    {
        qDebug( "Updated serial_number as abnormal" );
    }
   db.close();
user8181384
  • 3
  • 1
  • 7
  • Is it guaranteed that `_boardFile` and `layer` always contains any record? When calling `first()` and `last()` method of `QStringList` (variable `boardNameFile` and `boardLayerName`), the list must not be empty. – putu Jan 31 '18 at 08:57
  • It seems you have corrupted heap. It could have happened earlier especially if "free() unsorted chunks" occurs on db.open(). You can try to catch corruption with special tools. More about tools at https://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors. – BiTOk Jan 31 '18 at 18:56

0 Answers0