6

Im writing a medium size application for storing data based on sqlite databases. I have created a dialog to add data to the database. before saving the data it will check for some conditions to make suer that the entered data is valide. it is a simple prosess. and easy to find faults. im sute that their is no error to crash the application in eny point. also the application compiles without errors. and sometimes it works well, but most probely it crashes and close.

  1. my first question is why does this kind of things happen. (sometimes it works well, sometimes it crashes in the same condition).

  2. how to find faults in this kind of situation. simply what I do is, I use to change the logic and change the code, thinking that it will change the binary and correct the errors.

    ex:-

    if(query.exec()){
         //codes here.
     }
    

    I will replace this code with

    bool ok=query.exec();
      if(ok){
           //code here....
     }
    

please help me in this sitution, I would be wery thankful in eny advice. I will add the newMember.h and newMember.cpp which crash the application. (this is the class of my new member dialog). if eny more data is needed tell me I will add them too.

in the error report, in the error signature it is mentioned ModName:qtgui4.dll , Offset: 000c14e6

newMember.h

       #ifndef NEWMEMBER_H
       #define NEWMEMBER_H

       #include "ui_newmember.h"


       class newMember : public QDialog, private Ui::newMember
       {
           Q_OBJECT

       private:
           QString path_1;
           QPixmap pic;

       public:
           newMember(QString str, QWidget *parent );


       public slots:
           void browse();
           void save_2();

      };

      #endif // NEWMEMBER_H

newMember.cpp

        #include "newmember.h"
        #include<QtGui>
        #include<QtSql/QSqlDatabase>
        #include <QSqlQuery>
        #include <QSqlError>

        newMember::newMember(QString str, QWidget *parent)
        {
              setupUi(this);
           lineEdit_7->setText(str);
           radioButton->setChecked(true); ;

           connect(pushButton,SIGNAL(clicked()),this,SLOT(browse()));
           connect(pushButton_2,SIGNAL(clicked()),this,SLOT(save_2()));
        }


        void newMember::browse(){
             path_1=QFileDialog::getOpenFileName(this,"choose an image for the new house", QString::null,"Image Files (*.jpg *.bmp)");
             pic.load(path_1);
             pic=pic.scaled(284,213,Qt::KeepAspectRatio, Qt::SmoothTransformation);
             label_14->setPixmap(pic);
        }

        QString input1(QString str){
             if(str=="")
                 return "-NA-";
              else
                return str;
        }


       void newMember::save_2(){
             QByteArray array;
             QBuffer buf(&array);
             buf.open( QIODevice::WriteOnly);
             pic.save(&buf,"jpg");

             QString mof;
             if(radioButton->isChecked())mof="male";
             if(radioButton_2->isChecked())mof="female";

             QString isgm="false";
             if(checkBox->isChecked())isgm="true";


            QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName("data");
            db.open();

            QSqlQuery query;
            query.exec("create table members(aname text, homeno text, namein text, fname text, onames text, nic text, sex text, bday text,gm text,occupation text,contactno text,qulification text,note text, img BLOB) ");

             if(lineEdit_8->text()==""){
             QMessageBox::about(this,"error","you should enter a name to identify this member \n within the specific house");
             return;
             }

             query.prepare("select aname from members where homeno=? and aname=?   ");
             query.bindValue(0,lineEdit_7->text());
             query.bindValue(1,lineEdit_8->text());
             query.exec();
             if(query.next()){
             QMessageBox::about(this, "error", "the name you entered to identify this member \n  is already available, please enter another one") ;
             return;
            }

              if(isgm=="true"){
                  query.prepare("select aname from members where homeno=? and gm=?");
                  query.bindValue(0,lineEdit_7->text());
                  query.bindValue(1,"true");
                  query.exec();
                      if(query.next()){
                          QMessageBox::about(this, "error", "there is a gruha mulikaya set already");
                         return;
                        }
                }


                 query.prepare("insert into members(aname, homeno , namein , fname , onames , nic , sex , bday ,gm ,occupation ,contactno ,qulification ,note ,img) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)     ");
                 query.bindValue(0,lineEdit_8->text());
                 query.bindValue(1,input1(lineEdit_7->text()));
                 query.bindValue(2,input1(lineEdit->text()));
                 query.bindValue(3,input1(lineEdit_2->text()));
                 query.bindValue(4,input1(lineEdit_3->text()));
                 query.bindValue(5,input1(lineEdit_4->text()));
                 query.bindValue(6,mof);
                 query.bindValue(7,dateEdit->date().toString("yyyy-MM-dd") );
                 query.bindValue(8,isgm);
                 query.bindValue(9,input1(lineEdit_5->text()));
                 query.bindValue(10,input1(lineEdit_6->text()));
                 query.bindValue(11,input1(textEdit->toPlainText()));
                 query.bindValue(12,input1(textEdit_2->toPlainText()));
                 query.bindValue(13,array);


                bool ok=query.exec();
                if(!ok){
                   QSqlError error;
                   error=query.lastError();
                   QMessageBox::about(this,"error",error.text()   );
                 }
                else{
                    QMessageBox::about(this,"message","data added successfully");
                    newMember::close();
                 }

              }
Daniel F
  • 13,684
  • 11
  • 87
  • 116
danial weaber
  • 846
  • 2
  • 17
  • 37
  • 4
    Run the application in the debugger, which will tell you where it crashes. – Nikos C. Dec 26 '12 at 12:55
  • I have no knoladge in debugging Qt applications. enyway I will try. and give the results. thanks. – danial weaber Dec 26 '12 at 13:01
  • @NikosC. in application output it tells `The program has unexpectedly finished.` befoe that line it has 4 lines as `QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.` – danial weaber Dec 26 '12 at 13:24
  • 1
    For the "duplicate connection" problem, you should move the code that opens the database to the constructor, so that it only gets executed once (see [that answer](http://stackoverflow.com/a/7689551/894321) for details). – alexisdm Dec 26 '12 at 15:24

1 Answers1

4
  1. Try to run your app under debugger. It will show where in code your app crashes. In your case it is likely segfault.
  2. Why don't you check return values?
  • see my comment above it was my debugger output. their are no importan points to check return values in the code, isnt it. what is a segfault. please help – danial weaber Dec 26 '12 at 13:58
  • 1
    Segfault == Segmentation fault == Access Violation. You receive when trying to access memory that processor cannot address. E.g.: referencing null pointer. –  Dec 26 '12 at 14:08