Results 1 to 9 of 9

Thread: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

  1. #1
    Join Date
    Jan 2021
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    Hello everyone,

    I am using QT to manage a connection to a MS Access Database. It is reading data without issue, issue comes after using a record to

    Code is below. I get the exception in attached image when the records destructor is called. I tried calling clear on the record after use, but that also created a issue.

    ErrorMsg.jpg

    Below is method concerned


    Qt Code:
    1. void DataManager::setRelationalTableData(std::unique_ptr<tableData> data)
    2. {
    3. if (HelperFunctions::tableNameMatchesEnum(mCurrentRelationalTableModel->tableName(), data->tableName))
    4. {
    5. mCurrentRelationalTableModel->setEditStrategy(QSqlTableModel::EditStrategy::OnManualSubmit);
    6.  
    7. for (ushort row = 0; row < mCurrentRelationalTableModel->rowCount(); row++)
    8. {
    9. auto relateTableRecord = mCurrentRelationalTableModel->record(row);
    10. auto tableData = data->tableContents.find(row);
    11.  
    12. for (ushort dataItr = 0; dataItr < data->tableContents.size(); dataItr++)
    13. {
    14. relateTableRecord.setValue(QString::fromStdString(data->tableHeaders.at(dataItr)), QString::fromStdString(tableData->second.at(dataItr)));
    15. }
    16. mCurrentRelationalTableModel->setRecord(row, relateTableRecord);
    17. }
    18. mCurrentRelationalTableModel->submitAll();
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    EDIT:: The relationaltablemodel is being updated with new data, although this is never submitted as the crash occurs before submitall

    Many thanks in advance

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    issue comes after using a record to
    to what?

    in attached image
    Your image is completely unreadable. Copy and paste the text of the error, don't take a screenshot.

    There is nothing in the code you have posted that is helpful in solving the problem. Everything you show uses methods and data structures that you haven't shown, so there isn't much to go on.

    Are you running this code in the debugger? Where does the exception occur? What are the values of "data" and all of the things you are retrieving through the "data" pointer? If "data" or anything contained inside it is not valid, that could cause a crash.

    You are using std:: unique_ptr to hold the pointer to your "data" instance. Have you called operator= on this unique_ptr at some point? If you have, that means the pointer instance it previously held gets deleted, so "data" could be a dangling invalid pointer by the time your method is entered.

    Edit: It just occurred to me that you are passing your std:: unique_ptr<> by value and not by reference. This could be causing a reassignment of the original unique_ptr to a temporary created as the argument to the method, resulting in the wrapped pointer being deleted on the way in. Try changing the function definition to take a reference:

    Qt Code:
    1. ( std::unique_ptr< tableData > & data )
    To copy to clipboard, switch view to plain text mode 

    and if data isn't changed, make the argument const as well.
    Last edited by d_stranz; 10th January 2021 at 18:37.
    <=== 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.

  3. #3
    Join Date
    Jan 2021
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    Thanks for your feedback.

    Understand there is a lot of stuff there that is not explained, wasnt really sure what bits would be pertinent. One that clearly is and i didnt explain is the mCurrentRelationalTableModel is just a QSqlRelationalTableModel, not subclassed. When a request is made to DataManager (which this function lies within) mCurrentRelationalTableModel is set, and the user then makes changes to the data. When they commit the changes the data is sent to DataManager which then puts any changes into the mCurrentRelationalTableModel to submit.

    Oh blimey, not sure what happened there with the sentence. That sentence should read "..issue comes after successfully setting the relationaltablemodel (mCurrentRelationalTableModel ) with the contents of the record, when the for loop looses scope, in the destructor of the record "

    Apologies for the image, that is definately unreadable and much smaller than the one i uploaded, i was unaware of this issue.

    The only information provided by Debugger is that the application triggered a breakpoint.
    The breakpoint is in qvector.h, on the destruct line in the below.
    Looking at the call stack, it is when the for loop ends, and relateTableRecord destructor is being called. This occurs after mCurrentRelationalTableModel is successfully set with the new data

    Qt Code:
    1. template <typename T>
    2. void QVector<T>::freeData(Data *x)
    3. {
    4. destruct(x->begin(), x->end());
    5. Data::deallocate(x);
    6. }
    To copy to clipboard, switch view to plain text mode 



    The unique pointer for data is valid, as the record is succcessfully set to the qrelationaltablemodel. The unique pointer cant be copied so it is passed to this function by the caller using std::move. I dont believe there is a error in data reaching this function as data just contians some std::strings so is easy to observe.

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

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    You can first try to comment out the inner for loop to see if it still happens.

  5. #5
    Join Date
    Jan 2021
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    It does not happen if the line in the inner loop is commented out

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

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    Then put some debug output into the loop and/or only add some values and not all to see what happens.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    using std::move
    Yes, move semantics would be OK.

    data->tableContents.size()
    Is this a constant within the scope of the inner loop?

    auto tableData = data->tableContents.find(row);
    You dereference tableData inside the loop. Is this guaranteed to be a valid pointer?

    This whole thing could be a red herring. too. It might be that there is an error somewhere earlier that has corrupted memory or the stack and is only manifest at this point in the code.
    <=== 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.

  8. #8
    Join Date
    Jan 2021
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    Thank you all.

    I was left with two things to test, just took me a while to get around to it.

    So i first wondered if i was setting a part of the relational database which was linked to a foreign key. Although this would probably cause issues later on, it was not causing this issue.

    The issue was simple (and rather daft), i was trying to set a numeric parameter in the database with a string.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using QSqlRecord to update QSqlRelationalTableModel - exception on destructor

    The issue was simple (and rather daft), i was trying to set a numeric parameter in the database with a string.
    None of us have ever made that kind of mistake :-)
    <=== 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.

Similar Threads

  1. QSqlRelationalTableModel fail update
    By Alexandru in forum Qt Programming
    Replies: 0
    Last Post: 13th June 2018, 18:08
  2. Destructor for Qtconcurrent::Exception?
    By bp45738 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2011, 19:25
  3. Replies: 2
    Last Post: 26th October 2010, 13:38
  4. Unable to modify QSqlRecord in QSqlRelationalTableModel
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2010, 00:17
  5. Replies: 1
    Last Post: 7th June 2008, 19:29

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.