Results 1 to 7 of 7

Thread: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

  1. #1
    Join Date
    Jan 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    The following program paints a red background. Pressing the left mouse button paints a white rectangle onto it. The rectangle has a child rectangle and a QGraphicsDropShadowEffect. (QGraphicsOpacityEffect and QGraphicsColorizeEffect also lead to the problem, but less frequently.)

    Pressing the right mouse button removes the white rectangle.

    Sometimes when removing the rectangle it causes a segmentation fault.

    This does not happen if the QGraphicsDropShadowEffect is not applied. It also does not happen when there either is no child item in 'MyRect' or the background is ommited.

    (When searching for this issue, I found several hints that a segfault like this could be related to changing the 'boundingRect()' of an item without calling 'prepareGeometryChange()'.)

    I am really at a loss here. This is part of a bigger project and I boiled it down to the following example. This is on Debian, Qt 5.7.1+dfsg-3, GCC 6.2.1-5.

    Main.cc:
    Qt Code:
    1. #include<QApplication>
    2. #include<QGraphicsView>
    3. #include<QGraphicsScene>
    4. #include<QGraphicsSceneMouseEvent>
    5. #include<QGraphicsRectItem>
    6. #include<QGraphicsDropShadowEffect>
    7. #include<QScreen>
    8.  
    9. class MyRect: public QGraphicsRectItem
    10. {
    11. public:
    12. MyRect(QGraphicsItem* parent = nullptr):
    13. QGraphicsRectItem{QRectF{0.0f, 0.0f, 100.0f, 100.0f}, parent}
    14. {
    15. setPen(QPen{Qt::white});
    16. setBrush(QBrush{Qt::white, Qt::SolidPattern});
    17. new QGraphicsRectItem{this};
    18. }
    19. };
    20.  
    21. QGraphicsView* view=nullptr;
    22. QGraphicsRectItem* background=nullptr;
    23. MyRect* global_rect=nullptr;
    24.  
    25. void
    26. createRect(QGraphicsScene* scene)
    27. {
    28. if (global_rect==nullptr)
    29. {
    30. global_rect=new MyRect{};
    31. auto shadow=new QGraphicsDropShadowEffect{};
    32. shadow->setBlurRadius(15.0f);
    33. global_rect->setGraphicsEffect(shadow);
    34. scene->addItem(global_rect);
    35. global_rect->setPos(global_rect->mapToParent(global_rect->mapFromScene(view->mapToScene(view->mapFromGlobal(QCursor::pos())))));
    36. }
    37. }
    38.  
    39. void
    40. destroyRect(QGraphicsScene* scene)
    41. {
    42. if (global_rect!=nullptr)
    43. {
    44. scene->removeItem(global_rect);
    45. delete global_rect;
    46. global_rect=nullptr;
    47. }
    48. }
    49.  
    50. class MyScene: public QGraphicsScene
    51. {
    52. public:
    53. void mouseReleaseEvent(QGraphicsSceneMouseEvent* me) override
    54. {
    55. if (me->button()==Qt::LeftButton)
    56. {
    57. createRect(this);
    58. }
    59. else if (me->button()==Qt::RightButton)
    60. {
    61. destroyRect(this);
    62. }
    63. else
    64. {
    65. QGraphicsScene::mouseReleaseEvent(me);
    66. }
    67. }
    68. };
    69.  
    70. int
    71. main(int argc, char** argv)
    72. {
    73. QApplication qapp{argc, argv};
    74.  
    75. view=new QGraphicsView{};
    76. MyScene scene;
    77.  
    78. QRect rect=QGuiApplication::primaryScreen()->geometry();
    79. scene.setSceneRect(0.0f, 0.0f, rect.width(), rect.height());
    80. view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    81. view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    82. view->setFrameShape(QFrame::NoFrame);
    83. view->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
    84. view->setScene(&scene);
    85. view->showFullScreen();
    86.  
    87. background=new QGraphicsRectItem{0.0f, 0.0f, 500.0f, 500.0f};
    88. background->setPen(QPen{Qt::white});
    89. background->setBrush(QBrush{Qt::red, Qt::SolidPattern});
    90. scene.addItem(background);
    91.  
    92. return qapp.exec();
    93. }
    To copy to clipboard, switch view to plain text mode 

    Compiler call:

    Qt Code:
    1. g++ --std=c++14 -fPIC -Wall -Woverloaded-virtual -Werror -pedantic -g -O0 -fPIC -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/include/x86_64-linux-gnu/qt5 -o test Main.cc -lQt5Gui -lQt5Core -lQt5Widgets
    To copy to clipboard, switch view to plain text mode 

    Backtrace:

    Qt Code:
    1. #0 0x00007ffff71a1b84 in QGraphicsItem::parentItem() const () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    2. #1 0x00007ffff71a1bab in QGraphicsItem::topLevelItem() const () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    3. #2 0x00007ffff71eab6e in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    4. #3 0x00007ffff71e9ba6 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    5. #4 0x00007ffff71e9bfa in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    6. #5 0x00007ffff71e9bfa in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    7. #6 0x00007ffff71e9ba6 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    8. #7 0x00007ffff71e9c83 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    9. #8 0x00007ffff71ecd3c in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    10. #9 0x00007ffff71ed007 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    11. #10 0x00007ffff71dfc0d in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    12. #11 0x00007ffff7201aaa in QGraphicsView::paintEvent(QPaintEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    13. #12 0x00007ffff6f0f218 in QWidget::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    14. #13 0x00007ffff6ff799e in QFrame::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    15. #14 0x00007ffff72005bb in QGraphicsView::viewportEvent(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    16. #15 0x00007ffff76506f1 in QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    17. #16 0x00007ffff6ec7b05 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    18. #17 0x00007ffff6ecf2e1 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    19. #18 0x00007ffff7650990 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    20. #19 0x00007ffff6f07f7a in QWidgetPrivate::sendPaintEvent(QRegion const&) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    21. #20 0x00007ffff6f085e6 in QWidgetPrivate::drawWidget(QPaintDevice*, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*) ()
    22. from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    23. #21 0x00007ffff6ed7ebe in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    24. #22 0x00007ffff6ed80e7 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    25. #23 0x00007ffff6ef6f2f in QWidgetPrivate::syncBackingStore() () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    26. #24 0x00007ffff6f0f2e8 in QWidget::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    27. #25 0x00007ffff6ff799e in QFrame::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    28. #26 0x00007ffff7080d83 in QAbstractScrollArea::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    29. #27 0x00007ffff6ec7b2c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    30. #28 0x00007ffff6ecf2e1 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    31. #29 0x00007ffff7650990 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    32. #30 0x00007ffff765311d in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    33. #31 0x00007ffff71d1c32 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    34. #32 0x00007ffff71d74a9 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    35. #33 0x00007ffff767d449 in QObject::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    36. #34 0x00007ffff71e407b in QGraphicsScene::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    37. #35 0x00007ffff6ec7b2c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    38. #36 0x00007ffff6ecf2e1 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
    39. #37 0x00007ffff7650990 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    40. #38 0x00007ffff765311d in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    41. #39 0x00007ffff76a4bf3 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    42. #40 0x00007ffff446e7f7 in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
    43. #41 0x00007ffff446ea60 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
    44. #42 0x00007ffff446eb0c in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
    45. #43 0x00007ffff76a4fff in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    46. #44 0x00007ffff764e97a in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    47. #45 0x00007ffff76570ec in QCoreApplication::exec() () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
    48. #46 0x0000555555557b10 in main (argc=1, argv=0x7fffffffe5b8) at Main.cc:92
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    This is some of the strangest code I have seen in a while. Not only are you using global variables for some unknown reason, you are doing things like (in line 17 of Main.cc) creating a new QGraphicsRectItem without assigning the resulting pointer to anything.

    You have standalone methods to modify the scene which should be class methods of MyScene, not standalone. Your view is a global pointer instead of being declared a stack variable in main() like your MyScene variable is. The "global_rect" and "background" global variables shouldn't be global but should probably be members of the MyScene class.

    I'd suggest you refactor your code to remove all the strange things and maybe your crashes will clear up with them.
    <=== 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 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    I can't seem to find the button to edit my post. I have rewritten the code but the problem persists.

    Qt Code:
    1. #include<QApplication>
    2. #include<QGraphicsView>
    3. #include<QGraphicsScene>
    4. #include<QGraphicsSceneMouseEvent>
    5. #include<QGraphicsRectItem>
    6. #include<QGraphicsDropShadowEffect>
    7. #include<QScreen>
    8.  
    9. class MyRect: public QGraphicsRectItem
    10. {
    11. public:
    12. MyRect(QGraphicsItem* parent = nullptr):
    13. QGraphicsRectItem{QRectF{0.0f, 0.0f, 100.0f, 100.0f}, parent}
    14. {
    15. setPen(QPen{Qt::white});
    16. setBrush(QBrush{Qt::white, Qt::SolidPattern});
    17. my_child_=new QGraphicsRectItem{this};
    18. }
    19.  
    20. private:
    21. QGraphicsRectItem* my_child_=nullptr;
    22. };
    23.  
    24. class MyScene: public QGraphicsScene
    25. {
    26. public:
    27. MyScene()
    28. {
    29. background_=new QGraphicsRectItem{0.0f, 0.0f, 500.0f, 500.0f};
    30. background_->setPen(QPen{Qt::white});
    31. background_->setBrush(QBrush{Qt::red, Qt::SolidPattern});
    32. addItem(background_);
    33. }
    34.  
    35. void mouseReleaseEvent(QGraphicsSceneMouseEvent* me) override
    36. {
    37. if (me->button()==Qt::LeftButton)
    38. {
    39. if (my_rect_==nullptr)
    40. {
    41. my_rect_=new MyRect{};
    42. shadow_=new QGraphicsDropShadowEffect{};
    43. shadow_->setBlurRadius(15.0f);
    44. my_rect_->setGraphicsEffect(shadow_);
    45. addItem(my_rect_);
    46. my_rect_->setPos(me->scenePos());
    47. }
    48. }
    49. else if (me->button()==Qt::RightButton)
    50. {
    51. if (my_rect_!=nullptr)
    52. {
    53. removeItem(my_rect_);
    54. my_rect_->setGraphicsEffect(0);
    55. shadow_=nullptr;
    56. delete my_rect_;
    57. my_rect_=nullptr;
    58. }
    59. }
    60. else
    61. {
    62. QGraphicsScene::mouseReleaseEvent(me);
    63. }
    64. }
    65.  
    66. private:
    67. QGraphicsRectItem* background_=nullptr;
    68. MyRect* my_rect_=nullptr;
    69. QGraphicsDropShadowEffect* shadow_=nullptr;
    70. };
    71.  
    72. int
    73. main(int argc, char** argv)
    74. {
    75. QApplication qapp{argc, argv};
    76.  
    77. MyScene scene;
    78.  
    79. QRect rect=QGuiApplication::primaryScreen()->geometry();
    80. scene.setSceneRect(0.0f, 0.0f, rect.width(), rect.height());
    81. view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    82. view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    83. view.setFrameShape(QFrame::NoFrame);
    84. view.setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
    85. view.setScene(&scene);
    86. view.showFullScreen();
    87.  
    88. return qapp.exec();
    89. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    I can't really see anywhere in this code that would cause a crash. What happens if you start the program and then immediately exit? (In other words, don't click the mouse in the view).
    <=== 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.

  5. #5
    Join Date
    Jan 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    It is probably a bug in Qt. A possibly explanation and a workaround can be found here:

    https://forum.qt.io/topic/75510/dele...ds-to-segfault

  6. #6
    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: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    Hmmm. It is considered to be pretty rude to cross-post the same question to different forums. Had you not given that link, probably very few people here would know. So, we're wasting our time trying to find an answer to your problem here at the same time that you and others are working on the same thing somewhere else.
    <=== 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.

  7. #7
    Join Date
    Jan 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Deleting QGraphicsItem with QGraphicsEffect leads to segfault

    I'm sorry, I didn't mean to be rude. Thank you for your answer that you see no problem in my code.

    But I cannot see how anyone's time has been wasted. Your time would have been wasted if I had continued the topic here and not posted the link that it has been resolved.

    Also there wasn't really an issue to "work on". My question was whether or not my code is correct. I'm grateful for your assessment, that you see nothing wrong; others went further and stated quite clearly that the code is correct and the error must be in Qt and provided a workaround from their experience, which I shared with you.

Similar Threads

  1. QGraphicsEffect for nested groups
    By japcrword in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2015, 08:50
  2. Serializing a QHash leads to SEGFault
    By gbonnema in forum Newbie
    Replies: 10
    Last Post: 7th January 2015, 19:00
  3. Crash using QGraphicsEffect with QGraphicsItemGroup
    By akiross in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2011, 19:02
  4. QGraphicsEffect on a Phonon::VideoPlayer
    By scary_jeff in forum Newbie
    Replies: 3
    Last Post: 20th January 2011, 02:12
  5. Deleting a scene from QGraphicsItem mouseEvent
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2007, 12:27

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.