Results 1 to 6 of 6

Thread: Crash on QString assignment

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question Crash on QString assignment

    I'm pretty new to Qt and C++ so I'm probably missing something obvious, but for the life of me I can't figure out what is going wrong. One of the classes in a program I wrote is causing a crash whenever I try to do a basic string assignment. See below (slightly pared down).
    Qt Code:
    1. class AutochargeData : public QWidget
    2. {
    3.  
    4. public:
    5. AutochargeData(appData *appInfo, QWidget *parent = 0);
    6.  
    7. QString orderNum;
    8. QString paymentMethod;
    9. QString total;
    10.  
    11. void setPayment(QString payment);
    12. void setOrderID(QString orderID);
    13. void setTotal(QString total);
    14.  
    15. void getPaymentData(QString orderID);
    16.  
    17. private:
    18. appData *appInfo;
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    So in the below code, I have tried many variations. I know the query is fine because I've used qDebug() to check the output. I have tried skipping the set functions and doing a direct assignment, I have tried using the "this" keyword, and I have even tried just assigning a random string directly to the variables.

    Qt Code:
    1. AutochargeData::AutochargeData(appData *appInfo, QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. this->appInfo = appInfo;
    5. }
    6.  
    7. void AutochargeData::getPaymentData(QString orderID)
    8. {
    9. QString queryString = "SELECT order_payment.method, order.increment_id, order.grand_total FROM order JOIN order_payment ON sales_flat_order.entity_id = order_payment.parent_id WHERE order.increment_id = '" + orderID + "';";
    10.  
    11. QSqlQuery query(queryString, QSqlDatabase::database("db"));
    12. query.exec();
    13.  
    14. while (query.next()) {
    15. setOrderID(query.value("increment_id").toString());
    16. setPayment(query.value("method").toString());
    17. setTotal(query.value("grand_total").toString());
    18. }
    19. }
    20.  
    21. void AutochargeData::setPayment(QString payment)
    22. {
    23. paymentMethod = payment;
    24. }
    25.  
    26. void AutochargeData::setOrderID(QString orderID)
    27. {
    28. orderNum = orderID;
    29. }
    30.  
    31. void AutochargeData::setTotal(QString grandTotal)
    32. {
    33. total = grandTotal;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Any time I tried to assign a string to the orderNum, paymentMethod, or total variables, the program crashes. The debugger is ending on
    Qt Code:
    1. QString::operator=(const char*)
    To copy to clipboard, switch view to plain text mode 
    in the qstring.h file. As I mentioned, I am relatively new at this so it may be something obvious that I am missing. But after multiple hours of googling and trying different variations, I am stumped. Any thoughts would be much appreciated, and please let me know if there's more information that would be helpful.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash on QString assignment

    That usually means that "this" is not a valid object.
    I.e. that you are calling a method on a pointer that does not actual point to a valid object.

    E.g. an uninitialized pointer or a deleted pointer.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ce_nort (25th March 2016)

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

    Default Re: Crash on QString assignment

    I would look at the lifetime of the "appData" pointer. You are simply storing it in your AutochargeData class. If it goes out of scope (e.g. is deleted) before the AutochargeData class instance does, then it is an invalid pointer and if used will cause a crash.

    Qt Code:
    1. while (query.next()) {
    2. setOrderID(query.value("increment_id").toString());
    3. setPayment(query.value("method").toString());
    4. setTotal(query.value("grand_total").toString());
    5. }
    To copy to clipboard, switch view to plain text mode 

    This code makes no sense. If the query result has more than one result in it, each time through the loop you are completely replacing the values in your member variables (which themselves should not be public members of the class). At the end of the loop they will simply contain the values from the last result. I don't imagine that is your intent.

    Also not really sure why you have derived AutochargeData from QWidget. It doesn't appear to have any user interface, so why? Deriving from QWidget (or QObject for that matter) is unnecessary for pure C++ classes used in a Qt application. If you do derive from a QObject-based class (like QWidget), then you need to include the "Q_OBJECT" macro at the top of the class definition:

    Qt Code:
    1. class MyClass : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyClass( QObject * parent );
    7.  
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 24th March 2016 at 16:40.

  5. The following user says thank you to d_stranz for this useful post:

    ce_nort (25th March 2016)

  6. #4
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Crash on QString assignment

    Thanks for all the info, I am definitely new at this and self-taught/teaching so it is very helpful. I actually do have the "Q_OBJECT" macro in at the top of the class definition, I just left it out of the copy and paste. I'm not using signals and slots in this class, but I generally add it by default (bad idea?). I agree that the while loop would make no sense if the query returned more than one set of values, but it does not. There should be only one set of data to assign to the member variables, and the while loop was my understanding of how to check that the query returned successfully. I certainly don't claim that this is well written - I'm still learning what that looks like. It sounds like the general consensus is that the appData pointer is the issue and is probably deleted too soon elsewhere in the program. I'll check it out! Thanks again.

  7. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Crash on QString assignment

    IMO, the problem isn't in Q_OBJECT (as long as AutochargeData does not use "Qt extras" - it is not an UI object, it declares no properties, slots or signals, etc.) The setOrderID() method is innocent. IMO, the problem is somewhere in the database. For starters, I recommend splitting the setOrderID() statement and seeing whether you pass and what values will the debugger show. Therefore:
    Qt Code:
    1. QVariant val = query.value("increment_id"); // does it pass?
    2. QString str = val.toString(); // does it pass? What is in str?
    3.  
    4. setOrderID(str); // does it pass? What happens?
    To copy to clipboard, switch view to plain text mode 
    Pleas, post your findings.

  8. #6
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Crash on QString assignment

    I got this figured out shortly before you posted. As predicted, it was something obvious that I was missing. I had created an instance of AutochargeData in the Mainwindow class, but was trying to use it in a different class without creating another instance of it. So it was invalid, as was guessed by the other posts in this thread. Thanks everyone!

Similar Threads

  1. QString exception while executing assignment operation
    By Giox79 in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2015, 13:30
  2. Replies: 6
    Last Post: 29th December 2011, 19:37
  3. Comparing QString causes the program to crash .
    By ladiesfinger in forum Qt Programming
    Replies: 9
    Last Post: 6th November 2010, 18:17
  4. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 18:31
  5. Crash on QString Destructor
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 15:28

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.