Results 1 to 15 of 15

Thread: qstring assignment crashes the program

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default qstring assignment crashes the program

    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 :-
    -------------------
    Qt Code:
    1. product_record* product_record::product_search(int numb)
    2. {
    3. QString sql = "select * from tableProductRecords where number = ?";
    4. QSqlQuery qry;
    5. qry.prepare(sql);
    6.  
    7. qry.bindValue(0, numb);
    8.  
    9. if(qry.exec())
    10. {
    11. if(qry.next())
    12. {
    13. this->name = qry.value(0).toString();// here program crashes
    14. stock = qry.value(1).toInt();
    15. rate = qry.value(2).toFloat();
    16. number = qry.value(3).toInt();
    17. }
    18. }
    19. return this;
    20. }
    To copy to clipboard, switch view to plain text mode 
    this is mine header file :-
    ---------------------------------
    Qt Code:
    1. #ifndef PRODUCT_RECORD_H
    2. #define PRODUCT_RECORD_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QSqlDatabase>
    7. #include <QSqlQuery>
    8. #include <QVariant>
    9. #include <QMessageBox>
    10. #include <QSqlError>
    11.  
    12.  
    13. class product_record : public QObject
    14. {
    15. Q_OBJECT
    16.  
    17. QString name;
    18. int stock;
    19. float rate;
    20. int number;
    21.  
    22. public:
    23. product_record();
    24. ~product_record();
    25.  
    26. product_record * product_search(int numb);
    27. void product_modify(int no);
    28.  
    29. // getters and setters
    30.  
    31. QString text_name();
    32. int text_stock();
    33. float text_rate();
    34. int text_number();
    35.  
    36. void setText_name(QString *str);
    37. void setText_stcok(int *i);
    38. void setText_rate(float *f);
    39. void setText_number(int *n);
    40. };
    41.  
    42. #endif // PRODUCT_RECORD_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Where do you call it? Are you sure the instance is valid?

  3. #3
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    i've called in different form "dialogAddNewRecord" and a button slot and having this private memeber :- ( product_record *prdRecord )
    Qt Code:
    1. void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
    2. {
    3. QString str = ui->lineEditNumber->text().trimmed();
    4. int s = str.toInt();
    5.  
    6.  
    7. if(s)
    8. {
    9. product_record * prd = prdRecord->product_search(s);
    10. ui->lineEditName->setText(prd->text_name());
    11. ui->lineEditStock->setText(QString::number(prd->text_stock()));
    12. ui->lineEditRate->setText(QString::number(prd->text_rate()));
    13. ui->lineEditNumber->setText(QString::number(prd->text_number()));
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Where do you initialize prdRecord ?

  5. #5
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    after your queston i initialized prdRedord here but having same problem:-

    constructor is :-
    ----------------
    Qt Code:
    1. product_record::product_record()
    2. {
    3. this->name = "";
    4. this->stock = 0;
    5. this->rate = 0.0;
    6. this->number = 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    function is :-
    Qt Code:
    1. void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
    2. {
    3. QString str = ui->lineEditNumber->text().trimmed();
    4. int s = str.toInt();
    5.  
    6. prdRecord = new product_record();
    7. if(s)
    8. {
    9. product_record * prd = product_record::product_search(s, prdRecord);
    10. ui->lineEditName->setText(prd->text_name());
    11. ui->lineEditStock->setText(QString::number(prd->text_stock()));
    12. ui->lineEditRate->setText(QString::number(prd->text_rate()));
    13. ui->lineEditNumber->setText(QString::number(prd->text_number()));
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Qt Code:
    1. product_record::product_search(s, prdRecord);
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    after you questioned ( where you initialized prdRecord) then I modify code and added prdRecord as argument of product_search.

    after modification :-
    Qt Code:
    1. product_record* product_record::product_search(int numb, product_record *prdRecord)
    2. {
    3.  
    4. QString sql = "select * from tableProductRecords where number = ?";
    5. QSqlQuery qry;
    6. qry.prepare(sql);
    7.  
    8. qry.bindValue(0, numb);
    9. if(qry.exec())
    10. {
    11. if(qry.next())
    12. {
    13. prdRecord->name = qry.value(0).toString().trimmed();// here program crashes
    14. prdRecord->stock = qry.value(1).toInt();
    15. prdRecord->rate = qry.value(2).toFloat();
    16. prdRecord->number = qry.value(3).toInt();
    17. }
    18. }
    19. return prdRecord;
    20. }
    To copy to clipboard, switch view to plain text mode 
    and here i called this with initialization of perdRecord
    Qt Code:
    1. void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
    2. {
    3. QString str = ui->lineEditNumber->text().trimmed();
    4. int s = str.toInt();
    5. prdRecord = new product_record();
    6.  
    7. if(s)
    8. {
    9.  
    10. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", QString::number(s) );
    11.  
    12. product_record * prd = product_record::product_search(s, prdRecord);
    13. ui->lineEditName->setText(prd->text_name());
    14. ui->lineEditStock->setText(QString::number(prd->text_stock()));
    15. ui->lineEditRate->setText(QString::number(prd->text_rate()));
    16. ui->lineEditNumber->setText(QString::number(prd->text_number()));
    17. }
    18. else
    19. {
    20. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rahulvishwakarma; 1st April 2020 at 14:05.

  8. #8
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Please also show the backtrace of the crash

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qstring assignment crashes the program

    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:

    Qt Code:
    1. product_record * prd = product_record::product_search(s, prdRecord);
    To copy to clipboard, switch view to plain text mode 

    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)?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #10
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    please tell me "how to get backtrace of crash". and also product_search is static member of class product_record;

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qstring assignment crashes the program

    product_search is static member of class product_record;
    Qt Code:
    1. product_record * product_search(int numb);
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. product_record * prd = new product_record( this );
    2. prd->product_search( 42 );
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. #12
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: qstring assignment crashes the program

    as you said I build non-static fuction as follows
    Qt Code:
    1. bool product_record::product_search(int numb)//, product_record *prdRecord)
    2. {
    3. QString sql = "select * from tableProductRecords where number = ?";
    4. QSqlQuery qry;
    5. qry.prepare(sql);
    6.  
    7. qry.bindValue(0, numb);
    8.  
    9. if(qry.exec())
    10. {
    11. if(qry.next())
    12. {
    13. this->name = qry.value(0).toString(); // still program crashes here
    14. this->stock = qry.value(1).toInt();
    15. this->rate = qry.value(2).toInt();
    16. this->number = qry.value(3).toInt();
    17. return true;
    18. }
    19. return false;
    20. }
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    and called in this :-
    Qt Code:
    1. void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
    2. {
    3. QString str = ui->lineEditNumber->text().trimmed();
    4. int s = str.toInt();
    5. prdRecord = new product_record(this);
    6.  
    7. if(s)
    8. {
    9. prdRecord->product_search(s);
    10. ui->lineEditName->setText(prdRecord->text_name());
    11. ui->lineEditStock->setText(QString::number(prdRecord->text_stock()));
    12. ui->lineEditRate->setText(QString::number(prdRecord->text_rate()));
    13. ui->lineEditNumber->setText(QString::number(prdRecord->text_number()));
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Change line :
    Qt Code:
    1. this->name = qry.value(0).toString(); // still program crashes here
    To copy to clipboard, switch view to plain text mode 
    for this lines :
    Qt Code:
    1. QString test = qry.value(0).toString(); // still program crashes here
    2. this->name = test;
    To copy to clipboard, switch view to plain text mode 
    What is happening now ?

  14. The following user says thank you to Lesiok for this useful post:

    rahulvishwakarma (4th April 2020)

  15. #14
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    thanks a lot you solved my problem i did llike this
    Qt Code:
    1. QString name = qry.value(0).toString();
    2. this->name = name;
    3.  
    4. int n = qry.value(1).toInt();
    5. this->stock = n;
    6.  
    7. float r = qry.value(2).toInt();
    8. this->rate = r;
    9.  
    10. int number = qry.value(3).toInt();
    11. this->number = number;
    To copy to clipboard, switch view to plain text mode 
    but i don't get the point how is it happening, Please explain.

  16. #15
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    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 ?

Similar Threads

  1. Crash on QString assignment
    By ce_nort in forum Newbie
    Replies: 5
    Last Post: 25th March 2016, 17:08
  2. QString exception while executing assignment operation
    By Giox79 in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2015, 13:30
  3. Replies: 6
    Last Post: 29th December 2011, 19:37
  4. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 18:31
  5. QMap <int, QGuardedPtr<Employee> > Crashes on Assignment ???
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2006, 08:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.