Results 1 to 5 of 5

Thread: Simple program segfaults. QListView? Out of ideas.

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    Default Simple program segfaults. QListView? Out of ideas.

    I'm making a program to present the user (me) with a list of filenames and the option to do a few things to them. But I'm still only at the point of just testing how QListView works. It ought to be really simple, but it's crashing and I'm stuck.

    I don't even know where exactly it crashes; it's after the app exec(). I think maybe some object is being destroyed, and later accessed, but I don't understand Qt very well. As for some guesses: I read that QListView doesn't take ownership of models, but it still crashes if QStringListModel is initialized with any parent. I'm not using QModelIndex yet so it can't be due do to sloppy use of one of those.

    The program happens to segfault in QTextEngine's itemize(). It won't crash if some statements are switched around, or if the strings have very slightly different contents, or if a messagebox is exec() before dialog.show(), etc. However, I assume those are coincidences related to how memory is laid out, or timing.

    What am I doing wrong? I'm out of ideas.

    It's Qt 4.7.0 on linux. Problem also happens with Qt 4.7.2.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtGlobal>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QDialog dialog;
    9. QVBoxLayout layout;
    10. QLabel label("label of stuff");
    11. QListView view;
    12.  
    13. list.append(QString("hello"));
    14. list.append(QString("The cat he"));
    15. model.setStringList(list);
    16. view.setModel(&model);
    17.  
    18. dialog.setLayout(&layout);
    19. layout.addWidget(&label);
    20. dialog.show(); // if this happens here it crashes
    21. layout.addWidget(&view);
    22.  
    23. //dialog.show(); // if this instead happens here it's "stable"
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple program segfaults. QListView? Out of ideas.

    No crash on my machine. You should rather create your widgets on a heap ( with operator new ), try it and see if it still crashes:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtGlobal>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QDialog * dialog = new QDialog();
    9. QVBoxLayout * layout = new QVboxLayout(dialog);
    10. QLabel * label = new QLabel("label of stuff",dialog);
    11. QListView * view = new QListView(dialog);
    12. QStringListModel * model = new QStringListModel(view);
    13.  
    14. list.append(QString("hello"));
    15. list.append(QString("The cat he"));
    16. model->setStringList(list);
    17. view->setModel(model);
    18.  
    19. dialog->setLayout(layout);
    20. layout->addWidget(label);
    21. dialog->show();
    22. layout->addWidget(view);
    23.  
    24. int res = a.exec();
    25.  
    26. delete dialog;
    27.  
    28. return res;
    29. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    Default Re: Simple program segfaults. QListView? Out of ideas.

    No luck, but thanks anyway. I wonder how smart it would be to use the program in one of the configurations that doesn't crash, but knowing it might be very close to doing so.

    I made a version that connected every QObject's destroyed() signal to a message box's exec(), and the message box never appeared. So I don't think it's a bad pointer anymore.
    Last edited by rpocnab; 1st April 2011 at 23:40.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple program segfaults. QListView? Out of ideas.

    Why would you like to add to the layout after the widget is after the dialog is shown?
    In your particular case everything will happen so fast you won't see the "empty" dialog anyway.

    If you really need to show half of the dialog and the rest sometime after, you can add all the widgets to layout and specifically hide the ones you don't want and then show them later.

    And follow stampede's advice and create widgets on the heap.

    //your code doesn't crash on Windows with Qt 4.7.2 - i can't test on Linux at the moment

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    Default Re: Simple program segfaults. QListView? Out of ideas.

    I didn't think the order would make a difference, and once I saw it could crash I wanted to know why. However, now I can see the order affects how the dialog is sized.

Similar Threads

  1. Debugging error of a simple program
    By Professor widget in forum Newbie
    Replies: 5
    Last Post: 11th February 2011, 23:24
  2. Simple C/C++ program
    By ct in forum General Programming
    Replies: 4
    Last Post: 25th October 2007, 14:11
  3. how to use progressBar in simple program
    By jyoti in forum Newbie
    Replies: 1
    Last Post: 1st December 2006, 12:16
  4. Writing a simple Draw program
    By neomax in forum General Discussion
    Replies: 1
    Last Post: 23rd November 2006, 06:53
  5. Release my simple program to other users ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2006, 23:42

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.