u/MadAndSadGuy

An Unknown Segfault

Hello,

I'm creating an application that is used to scan MTG Cards in real-time from 4 or more cameras, and output the nameplate list of each camera to a per-camera-window, which can then be captured in a streaming platform like OBS. It is in early stage and the actual detection/processing is not yet implemented, partially because of this segfault. It uses Qt, tbb queues and tbb::flow::graph.

https://github.com/ahsanullah-8bit/MTGScanner.git

At first made it work (with the integrated camera), a few problems occurred with QtMultimedia that I didn't know of (or partially known):

  • You can't have any QtMultimedia in any other thread than the main thread.
  • You can't use a camera device twice, at the same time.

The project uses a custom ChannelWizard dialog component that involves 3 steps. Step 1 lets you select a camera, it has a combo box and a live preview below that. First thought would be, to just use VideoOutput, MediaCaptureSession, MediaDevices and Camera QML types for the wizard camera selection. But when the wizard closes, we have to add the channel to the Engine and now you've to destroy the Camera and MediaCaptureSession, because they still hold the camera handle. Back then, I would initialize the QCamera and QMediaCaptureSession in a per-camera capture thread, called CameraMetrics (which lived in the main thread) methods from the tbb::flow::graph nodes and lot more synchronizarion problems. Now, we ask the engine (from QML) to provide us a Channel (custom type holding per-camera half-components, including QCamera and QMediaCaptureSession) object, owned by C++ and use it to display the preview, pass it back to C++ if channel is to be created or destroy it, if canceled. Channel is used for the main thread and ChannelRaw is used by the EngineWorker thread, as it holds the supposedly thread safe components. Both are connected by main thread giving frames, and worker thread giving metrics QAtomicInts.

The segfault occurred in the previous (commit 37913a9) and the current (commit 806aeba) architecture.

Before, if there were no channels, you add one, and launch the wizard again, it would not show anything in the preview (Camera probably fails silently) for the same camera. You close the app, let it save the channel configurations, launch it again, a simple click on the Add Channel button causes a segfault.

Now, with the QtMultimedia moved to the main thread, I sometimes, get it at the start, or clicking a channel, or Add Channel. Disabling ChannelWizard sometimes work. I can't debug it. It produces this call stack almost always:


2  QV4::Managed::mark                                                                                                                                                                            qv4managed_p.h                    178  0x7ffff6cda402

3  QV4::MemoryManager::collectFromJSStack                                                                                                                                                        qv4mm.cpp                         1474 0x7ffff6cda402

4  QV4::(anonymous namespace)::markJSStack                                                                                                                                                       unique_ptr.h                      173  0x7ffff6cda51b` 

5  QV4::GCStateMachine::transition                                                                                                                                                               qv4mm.cpp                         1567 0x7ffff6cdacb7` 

6  QV4::GCStateMachine::step                                                                                                                                                                     qv4mm_p.h                         89   0x7ffff6cdbd55

7  QV4::MemoryManager::runGC                                                                                                                                                                     qv4mm.cpp                         1289 0x7ffff6cdbd55

8  QV4::MemoryManager::allocate                                                                                                                                                                  qv4mm_p.h                         409  0x7ffff6cdeae4

9  QV4::MemoryManager::allocData                                                                                                                                                                 qv4mm.cpp                         1011 0x7ffff6cdeae4 

10 QV4::MemoryManager::allocManaged<QV4::CallContext>                                                                                                                                            qv4mm_p.h                         215  0x7ffff6bbc5c6

11 QV4::ExecutionContext::newCallContext                                                                                                                                                         qv4context.cpp                    74   0x7ffff6bbc5c6

12 QV4::Runtime::PushCallContext::call                                                                                                                                                           qv4runtime.cpp                    1758 0x7ffff6c84ebd

13 QV4::Moth::VME::interpret                                                                                                                                                                     qv4vme_moth.cpp                   926  0x7ffff6cd0344

14 QV4::Moth::VME::exec                                                                                                                                                                          qv4vme_moth.cpp                   493  0x7ffff6cd65e3

15 QV4::doCall                                                                                                                                                                                   qv4function.cpp                   53   0x7ffff6c09a8a

16 QV4::Function::call                                                                                                                                                                           qv4function.cpp                   78   0x7ffff6c09d21

17 QQmlJavaScriptExpression::evaluate                                                                                                                                                            qqmljavascriptexpression.cpp      238  0x7ffff6d7f293 

18 QQmlBinding::evaluate                                                                                                                                                                         qqmlbinding.cpp                   195  0x7ffff6d0de8d

19 QQmlBinding::doUpdate                                                                                                                                                                         qqmlbinding.cpp                   713  0x7ffff6d0dfa9

20 QQmlBinding::update                                                                                                                                                                           qqmlbinding.cpp                   165  0x7ffff6d117b2

21 QPropertyObserverPointer::notify                                                                                                                                                              qproperty_p.h                     916  0x7ffff4c2e1af

22 QObjectCompatProperty<QQuickItemPrivate, double, &QQuickItemPrivate::_qt_property_height_offset, &QQuickItemPrivate::setHeight, &QQuickItemPrivate::heightChanged, decltype(nullptr)>::notify qproperty_p.h                     671  0x7ffff4c2e1af

23 QQuickItem::geometryChange                                                                                                                                                                    qquickitem.cpp                    3921 0x7ffff4c1aabc

24 QQuickItem::setSize                                                                                                                                                                           qquickitem.cpp                    7790 0x7ffff4c11ff0

25 QQuickControlPrivate::resizeBackground                                                                                                                                                        qquickcontrol.cpp                 366  0x7ffff5309b1b

26 QQuickControl::geometryChange                                                                                                                                                                 qquickcontrol.cpp                 2134 0x7ffff5313ddd

27 QQuickItem::setSize                                                                                                                                                                           qquickitem.cpp                    7790 0x7ffff4c11ff0

28 QQuickGridLayoutItem::setGeometry                                                                                                                                                             qquickgridlayoutengine_p.h        86   0x7fffc3fbf196

29 QGridLayoutEngine::setGeometries                                                                                                                                                              qgridlayoutengine.cpp             1060 0x7ffff77acede

30 QQuickGridLayoutBase::rearrange                                                                                                                                                               qquicklinearlayout.cpp            479  0x7fffc3fbce66

31 QQuickLayout::geometryChange                                                                                                                                                                  qquicklayout.cpp                  980  0x7fffc3fb953d

32 QQuickItem::setSize                                                                                                                                                                           qquickitem.cpp                    7790 0x7ffff4c11ff0

33 QQuickAnchorsPrivate::setItemSize                                                                                                                                                             qquickanchors.cpp                 392  0x7ffff4bda0fa

34 QQuickAnchorsPrivate::fillChanged                                                                                                                                                             qquickanchors.cpp                 168  0x7ffff4bda0fa

35 operator()                                                                                                                                                                                    qquickitem.cpp                    3909 0x7ffff4c1aa25

36 QQuickItemPrivate::notifyChangeListeners<QQuickItem::geometryChange(const QRectF&, const QRectF&)::<lambda(const QQuickItemPrivate::ChangeListener&)>>                                        qquickitem_p.h                    416  0x7ffff4c1aa25

37 QQuickItem::geometryChange                                                                                                                                                                    qquickitem.cpp                    3907 0x7ffff4c1aa25

38 QQuickItem::setHeight                                                                                                                                                                         qquickitem.cpp                    7614 0x7ffff4c114ea

39 QQuickPagePrivate::relayout                                                                                                                                                                   qquickpage.cpp                    117  0x7ffff5335ec4

40 QQuickControl::geometryChange                                                                                                                                                                 qquickcontrol.cpp                 2135 0x7ffff5313dea

41 QQuickItem::setSize                                                                                                                                                                           qquickitem.cpp                    7790 0x7ffff4c11ff0

42 QQuickAnchorsPrivate::setItemSize                                                                                                                                                             qquickanchors.cpp                 392  0x7ffff4bda0fa

43 QQuickAnchorsPrivate::fillChanged                                                                                                                                                             qquickanchors.cpp                 168  0x7ffff4bda0fa

44 operator()                                                                                                                                                                                    qquickitem.cpp                    3909 0x7ffff4c1aa25

45 QQuickItemPrivate::notifyChangeListeners<QQuickItem::geometryChange(const QRectF&, const QRectF&)::<lambda(const QQuickItemPrivate::ChangeListener&)>>                                        qquickitem_p.h                    416  0x7ffff4c1aa25 

46 QQuickItem::geometryChange                                                                                                                                                                    qquickitem.cpp                    3907 0x7ffff4c1aa25

47 QQuickItem::setSize                                                                                                                                                                           qquickitem.cpp                    7790 0x7ffff4c11ff0

48 QQuickControlPrivate::resizeContent                                                                                                                                                           qquickcontrol.cpp                 377  0x7ffff5310594

49 QQuickControl::paddingChange                                                                                                                                                                  qquickcontrol.cpp                 2174 0x7ffff530a8f8

50 QQuickControlPrivate::setTopPadding                                                                                                                                                           qquickcontrol.cpp                 200  0x7ffff530a8f8

51 QQmlPropertyData::writeProperty                                                                                                                                                               qqmlpropertydata_p.h              413  0x7ffff6d1589b

52 QQmlPropertyPrivate::write                                                                                                                                                                    qqmlproperty.cpp                  1920 0x7ffff6dd85ca

53 QQmlPropertyPrivate::writeValueProperty                                                                                                                                                       qqmlproperty.cpp                  1328 0x7ffff6ddc1c4

54 QQmlPropertyToUnbindablePropertyBinding::update                                                                                                                                               qqmlpropertytopropertybinding.cpp 111  0x7ffff6df1c67

55 QQmlNotifier::emitNotify                                                                                                                                                                      qqmlnotifier.cpp                  74   0x7ffff6dacbdd

56 doActivate<false>                                                                                                                                                                             qobject.cpp                       4147 0x7ffff63e82cb

57 QMetaObject::activate                                                                                                                                                                         qobject.cpp                       4317 0x7ffff63e0867

58 QQuickSafeArea::marginsChanged                                                                                                                                                                moc_qquicksafearea_p.cpp          169  0x7ffff4c63304 

59 QQuickSafeArea::updateSafeArea                                                                                                                                                                qquicksafearea.cpp                293  0x7ffff4c63c4f

60 QQuickSafeArea::updateSafeAreasRecursively                                                                                                                                                    qquicksafearea.cpp                394  0x7ffff4c64ead 

61 QQuickSafeArea::setAdditionalMargins                                                                                                                                                          qquicksafearea.cpp                194  0x7ffff4c65003

62 QQuickApplicationWindowPrivate::relayout                                                                                                                                                      qquickapplicationwindow.cpp       176  0x7ffff52fa62b

63 operator()                                                                                                                                                                                    qquickitem.cpp                    3909 0x7ffff4c1aa25

64 QQuickItemPrivate::notifyChangeListeners<QQuickItem::geometryChange(const QRectF&, const QRectF&)::<lambda(const QQuickItemPrivate::ChangeListener&)>>                                        qquickitem_p.h                    416  0x7ffff4c1aa25

65 QQuickItem::geometryChange                                                                                                                                                                    qquickitem.cpp                    3907 0x7ffff4c1aa25

66 QQuickControl::geometryChange                                                                                                                                                                 qquickcontrol.cpp                 2133 0x7ffff5313dd0

67 QQuickItem::setWidth                                                                                                                                                                          qquickitem.cpp                    7406 0x7ffff4c10ada

68 layoutItem                                                                                                                                                                                    qquickapplicationwindow.cpp       125  0x7ffff52fa052

69 layoutItem                                                                                                                                                                                    qnumeric.h                        297  0x7ffff52fa425

70 QQuickApplicationWindowPrivate::relayout                                                                                                                                                      qquickapplicationwindow.cpp       160  0x7ffff52fa425

71 QQuickApplicationWindowPrivate::executeBackground                                                                                                                                             qquickapplicationwindow.cpp       294  0x7ffff52fae6a

72 QQuickApplicationWindow::componentComplete                                                                                                                                                    qquickapplicationwindow.cpp       822  0x7ffff52fda1b 

73 QQmlObjectCreator::finalize                                                                                                                                                                   qqmlobjectcreator.cpp             1617 0x7ffff6dafe42

74 QQmlComponentPrivate::complete                                                                                                                                                                qbipointer_p.h                    133  0x7ffff6d2d540

75 QQmlComponentPrivate::completeCreate                                                                                                                                                          qqmlcomponent.cpp                 1344 0x7ffff6d338eb

76 QQmlComponent::completeCreate                                                                                                                                                                 qqmlcomponent.cpp                 1310 0x7ffff6d34333 

77 QQmlComponentPrivate::createWithProperties                                                                                                                                                    qqmlcomponent.cpp                 998  0x7ffff6d34333

78 QQmlComponent::create                                                                                                                                                                         qqmlcomponent.cpp                 938  0x7ffff6d3463d

79 QQmlApplicationEnginePrivate::finishLoad                                                                                                                                                      qqmlapplicationengine.cpp         158  0x7ffff6d0cc93

80 QQmlApplicationEnginePrivate::ensureLoadingFinishes                                                                                                                                           qqmlapplicationengine.cpp         185  0x7ffff6d0ce9c

81 QQmlApplicationEnginePrivate::startLoad                                                                                                                                                       qqmlapplicationengine.cpp         144  0x7ffff6d0d236

82 main                                                                                                                                                                                         main.cpp                          29   0x5555555a08a2

Google AI Search said, these are the common causes:

  • Object Ownership Issues
  • Dangling Pointers in Loaders
  • Uninitialized C++ Objects
  • Module-Specific Bugs
  • Lifecycle of argc/argv

I know the call stack says, the garbage collector is having the segfault. But I looked at my code again and again, tried different LLMs, questions, posts, articles, and I'm exhausted. It might just be single line, mistake or misuse. If you got some free time on your hand, please have a look at it. Again, the project is still in development, so you might find some broken code.

It should compile on both Linux and Windows.

Thank you!

reddit.com
u/MadAndSadGuy — 4 days ago