PDA

View Full Version : qstring assignment crashes the program



rahulvishwakarma
31st March 2020, 12:06
hi to all, I've centso7.5 and Qt 5.7 in VM. i am tryng to build a program having following code
problem is that when assigning a query value in to a QString variable, program crashes. how to solve it.
mine function is :-
-------------------


product_record* product_record::product_search(int numb)
{
QString sql = "select * from tableProductRecords where number = ?";
QSqlQuery qry;
qry.prepare(sql);

qry.bindValue(0, numb);

if(qry.exec())
{
if(qry.next())
{
this->name = qry.value(0).toString();// here program crashes
stock = qry.value(1).toInt();
rate = qry.value(2).toFloat();
number = qry.value(3).toInt();
}
}
return this;
}
this is mine header file :-
---------------------------------


#ifndef PRODUCT_RECORD_H
#define PRODUCT_RECORD_H

#include <QObject>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QMessageBox>
#include <QSqlError>

class QMessageBox;

class product_record : public QObject
{
Q_OBJECT

QString name;
int stock;
float rate;
int number;

public:
product_record();
~product_record();

product_record * product_search(int numb);
void product_modify(int no);

// getters and setters

QString text_name();
int text_stock();
float text_rate();
int text_number();

void setText_name(QString *str);
void setText_stcok(int *i);
void setText_rate(float *f);
void setText_number(int *n);
};

#endif // PRODUCT_RECORD_H

ChristianEhrlicher
31st March 2020, 15:12
Where do you call it? Are you sure the instance is valid?

rahulvishwakarma
31st March 2020, 16:04
i've called in different form "dialogAddNewRecord" and a button slot and having this private memeber :- ( product_record *prdRecord )


void dialogAddNewRecord::on_pushButtonSearchByNumber_cl icked()
{
QString str = ui->lineEditNumber->text().trimmed();
int s = str.toInt();


if(s)
{
product_record * prd = prdRecord->product_search(s);
ui->lineEditName->setText(prd->text_name());
ui->lineEditStock->setText(QString::number(prd->text_stock()));
ui->lineEditRate->setText(QString::number(prd->text_rate()));
ui->lineEditNumber->setText(QString::number(prd->text_number()));
}
else
{
QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
}

}

ChristianEhrlicher
31st March 2020, 16:43
Where do you initialize prdRecord ?

rahulvishwakarma
31st March 2020, 18:42
after your queston i initialized prdRedord here but having same problem:-

constructor is :-
----------------


product_record::product_record()
{
this->name = "";
this->stock = 0;
this->rate = 0.0;
this->number = 0;
}


function is :-


void dialogAddNewRecord::on_pushButtonSearchByNumber_cl icked()
{
QString str = ui->lineEditNumber->text().trimmed();
int s = str.toInt();

prdRecord = new product_record();
if(s)
{
product_record * prd = product_record::product_search(s, prdRecord);
ui->lineEditName->setText(prd->text_name());
ui->lineEditStock->setText(QString::number(prd->text_stock()));
ui->lineEditRate->setText(QString::number(prd->text_rate()));
ui->lineEditNumber->setText(QString::number(prd->text_number()));
}
else
{
QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
}

}

ChristianEhrlicher
31st March 2020, 18:57
product_record::product_search(s, prdRecord);


This can't compile according to your code in the first post.
Please show the backtrace of the crash and the correct source code.

rahulvishwakarma
1st April 2020, 13:00
after you questioned ( where you initialized prdRecord) then I modify code and added prdRecord as argument of product_search.

after modification :-


product_record* product_record::product_search(int numb, product_record *prdRecord)
{

QString sql = "select * from tableProductRecords where number = ?";
QSqlQuery qry;
qry.prepare(sql);

qry.bindValue(0, numb);
if(qry.exec())
{
if(qry.next())
{
prdRecord->name = qry.value(0).toString().trimmed();// here program crashes
prdRecord->stock = qry.value(1).toInt();
prdRecord->rate = qry.value(2).toFloat();
prdRecord->number = qry.value(3).toInt();
}
}
return prdRecord;
}

and here i called this with initialization of perdRecord


void dialogAddNewRecord::on_pushButtonSearchByNumber_cl icked()
{
QString str = ui->lineEditNumber->text().trimmed();
int s = str.toInt();
prdRecord = new product_record();

if(s)
{

QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", QString::number(s) );

product_record * prd = product_record::product_search(s, prdRecord);
ui->lineEditName->setText(prd->text_name());
ui->lineEditStock->setText(QString::number(prd->text_stock()));
ui->lineEditRate->setText(QString::number(prd->text_rate()));
ui->lineEditNumber->setText(QString::number(prd->text_number()));
}
else
{
QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
}

}

ChristianEhrlicher
1st April 2020, 16:53
Please also show the backtrace of the crash

d_stranz
1st April 2020, 16:56
after you questioned ( where you initialized prdRecord) then I modify code and added prdRecord as argument of product_search.

After reading many of your posts in this forum, I wonder if you really understand C++. Randomly adding arguments that point to the same type as the class itself, and then returning that same pointer as a result is completely confused and circular logic.

And as ChristianErlicher said, this statement is incorrect and should not compile:


product_record * prd = product_record::product_search(s, prdRecord);

productSearch() is not a static method of the product_record class, so the only way to correctly call it is using a pointer to a valid instance of the product_record class.

I am also pretty sure that your code has memory leaks. If every time you do a query you create a new instance of product_record, do you delete the old one after you are finished with it, or does the pointer just get lost (and cause a memory leak)?

rahulvishwakarma
2nd April 2020, 13:38
please tell me "how to get backtrace of crash". and also product_search is static member of class product_record;

d_stranz
2nd April 2020, 18:02
product_search is static member of class product_record;


product_record * product_search(int numb);

Not in the code you originally posted. And it makes absolutely no sense to make this method static. Create an instance of your product_record class, and call the product_search() method using that pointer.



product_record * prd = new product_record( this );
prd->product_search( 42 );


If you are using the debugger to run and test your code, the stack trace is automatically produced when the code crashes. If you are using Qt Creator as your development IDE, the debug output window will be opened when you build and run in Debug mode.

If you aren't using the debugger, then you need to learn how to use it. You cannot find and fix bugs otherwise.

And pay attention to what the compiler is telling you as you build your program. If it warns about uninitialized variables, then that is something you must fix.

rahulvishwakarma
3rd April 2020, 13:55
as you said I build non-static fuction as follows


bool product_record::product_search(int numb)//, product_record *prdRecord)
{
QString sql = "select * from tableProductRecords where number = ?";
QSqlQuery qry;
qry.prepare(sql);

qry.bindValue(0, numb);

if(qry.exec())
{
if(qry.next())
{
this->name = qry.value(0).toString(); // still program crashes here
this->stock = qry.value(1).toInt();
this->rate = qry.value(2).toInt();
this->number = qry.value(3).toInt();
return true;
}
return false;
}


}


and called in this :-


void dialogAddNewRecord::on_pushButtonSearchByNumber_cl icked()
{
QString str = ui->lineEditNumber->text().trimmed();
int s = str.toInt();
prdRecord = new product_record(this);

if(s)
{
prdRecord->product_search(s);
ui->lineEditName->setText(prdRecord->text_name());
ui->lineEditStock->setText(QString::number(prdRecord->text_stock()));
ui->lineEditRate->setText(QString::number(prdRecord->text_rate()));
ui->lineEditNumber->setText(QString::number(prdRecord->text_number()));
}
else
{
QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
}

}

Lesiok
3rd April 2020, 15:04
Change line :
this->name = qry.value(0).toString(); // still program crashes herefor this lines :
QString test = qry.value(0).toString(); // still program crashes here
this->name = test;What is happening now ?

rahulvishwakarma
3rd April 2020, 17:11
thanks a lot you solved my problem i did llike this


QString name = qry.value(0).toString();
this->name = name;

int n = qry.value(1).toInt();
this->stock = n;

float r = qry.value(2).toInt();
this->rate = r;

int number = qry.value(3).toInt();
this->number = number;

but i don't get the point how is it happening, Please explain.

Lesiok
4th April 2020, 08:03
It makes no sense to me.
By the way.
1. You are not guaranteed that a SELECT * query will return the record columns in the specified order. Either construct the query by explicitly entering the column names or retrieve the values later via QSqlRecord and field names.

2. There should be no toFloat() on line number 7 ?