Results 1 to 6 of 6

Thread: Crash on QString assignment

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 15:40.

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

    ce_nort (25th March 2016)

  3. #2
    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.

  4. #3
    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.

  5. #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

    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, 12:30
  2. Replies: 6
    Last Post: 29th December 2011, 18:37
  3. Comparing QString causes the program to crash .
    By ladiesfinger in forum Qt Programming
    Replies: 9
    Last Post: 6th November 2010, 17:17
  4. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 17:31
  5. Crash on QString Destructor
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 14: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.