Results 1 to 6 of 6

Thread: attempting to access QStandardItemModel causes seg fault in some cases

  1. #1
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default attempting to access QStandardItemModel causes seg fault in some cases

    Hi All,

    I'm having a bizarre problem with QStandardItemModel. I've implemented a pointer to a model in my mainwindow class. In a setup function in that class I create a model and point that model to the member pointer in the class. In another function, I sucessfully set it up. In still another, I successfully manipulate it. In a third, it always causes a seg fault. So clearly I'm doing something wrong. I would have to reimplement a ton of code to make a simple project, so I'm hoping first with the below snippets that it will be obvious what I'm doing wrong. Any ideas? It's like it's a scope problem, but it's all in one class.

    Thanks,
    Brian


    Qt Code:
    1. In Mainwindow.h, as part of class:
    2.  
    3. QStandardItemModel * archive_model;
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. In mainwindow.cpp:
    2.  
    3. setup(){
    4. archive_model = new QStandardItemModel(this);
    5. btx_api->getRuns(*archive_model);
    6.  
    7. get_request(){
    8. foo = archive_model->item(ind.row(),0)->text();//foo has correct value "1"
    9. }
    10.  
    11. other_request(){
    12. foo = archive_model->item(ind.row(),0)->text();//causes seg fault.
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: attempting to access QStandardItemModel causes seg fault in some cases

    Maybe other_request is called before you have created the model? Check if archive_model is valid when calling other_request and also what value have ind.row()?

    From the snippet I see no error.

  3. #3
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: attempting to access QStandardItemModel causes seg fault in some cases

    Hi Lykurg,

    Good ideas-- Thanks! Unfortunately, using
    foo = archive_model->item(0,0)->text(); also causes a seg fault. I've checked that I'm not calling other_request() before the model is populated. Any other ideas? Is this a textbook way of using models-- that is, place the pointer in the class, create the model in a method of the class, access it in other methods using the pointer? I'm a newbie...

    Thanks,
    Brian

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: attempting to access QStandardItemModel causes seg fault in some cases

    I'm a newbie...
    One of the things that most newbies learn the hard way is to *never* assume that a pointer returned from a function call is valid. Blindly trusting that archive_model->item( 0, 0 ) will *always* return a valid pointer will lead you down the path to segmentation faults. Or assuming that ind.row() always returns a valid row index, or that "ind" itself is valid, or any of multiple assumptions you are making in your code.

    One of the characteristics of Qt's Model/View architecture is that invalid model indexes are a standard return value used to indicate that something being requested doesn't exist (like a parent of a given index, or an index at a given row and column). If you simply assume you will always get a valid index, then you'll end up where you are now.

    Check pointers to see that they are non-NULL, check all model indexes for validity (QModelIndex::isValid()), check everything that could give rise to an error even if you *know* there can't be anything wrong :-).

    If your archive_model pointer has been created in the constructor somewhere, you can probably safely assume it is valid (unless it could be deleted and left dangling somewhere else in your code, which leads to a seg fault), but if it is created somewhere else, then you need to initialize it to zero in the constructor (otherwise it contains garbage and points to a seg fault) and check it before you try to use it. Then you need to check that your index is valid before you try to access something in your model using it (otherwise a seg fault), then you need to check that the pointer returned from your model is valid (another seg fault opportunity), and then if the call to text() also returns a pointer, you need to check that too before using it (ditto).

    Either insert these error checks into your code, or use asserts() (which do nothing in release mode), or use the debugger to find out where and why the seg fault occurs.

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

    briankeeney (8th February 2013)

  6. #5
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: attempting to access QStandardItemModel causes seg fault in some cases

    Thanks! I think you're right on target with that. It certainly ended up being the problem in this case-- I was using the index from one model to call another, so I ran off the end of the model and was rightly smacked down for it.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: attempting to access QStandardItemModel causes seg fault in some cases

    Hmmm, I don't think that QModelIndex instances can be "shared" among models - an index into a model is specific to that model, and can't be used to refer to an item at the same location in a different model, if that's what you mean you are trying to do. You can extract the row and column numbers from the index and use them to retrieve the corresponding index from the other model, but then you still need to check that both the source and target indexes are valid.

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2011, 10:09
  2. Replies: 22
    Last Post: 2nd January 2011, 21:52
  3. Access ODBC SQLDisconnect Segmentation Fault
    By KaptainKarl in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2010, 14:29
  4. Qt 4.5 not using Oxygen style in some cases
    By pawelprazak in forum Newbie
    Replies: 1
    Last Post: 14th February 2009, 13:40
  5. QProcess not working in some cases
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2006, 09:58

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
  •  
Qt is a trademark of The Qt Company.