Results 1 to 12 of 12

Thread: Qt 5.4 sudden segfault when calling invokable c++ function from qml

  1. #1
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Hi,

    I have these two situations:
    Qt Code:
    1. class myClass
    2. {
    3. ...
    4. }
    5.  
    6. class myModel : public QAbstractTableMOdel
    7. {
    8. public:
    9. ...
    10. Q_INVOKABLE myClass getElement(int index)
    11. {
    12. if(0 > index || index >= m_Data.size())
    13. return 0;
    14.  
    15. return m_Data[index];
    16. }
    17.  
    18. private:
    19. QList<myClass> m_Data;
    20. }
    To copy to clipboard, switch view to plain text mode 
    calling the getElement(index) function from qml works just fine. However, if I change to pointers like so:
    Qt Code:
    1. class myClass : public QObject
    2. {
    3. Q_OBJECT
    4. ...
    5. }
    6.  
    7. class myModel : public QAbstractTableMOdel
    8. {
    9. public:
    10. ...
    11. Q_INVOKABLE myClass* getElement(int index)
    12. {
    13. if(0 > index || index >= m_Data.size())
    14. return 0;
    15.  
    16. return m_Data[index];
    17. }
    18.  
    19. private:
    20. QList<myClass*> m_Data;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Then it first works fine as well. But every now and then I get a segfault when calling the getElement(index) function from qml. I don't understand how it can work most of the time but suddenly create a segfault. Also, I have no clue how to debug it any further. I narrowed it down to the actual function call but I have no clue why it would give a segfault. Can anyone tell my why this might be happening?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Show us the backtrace, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Uhh, this is embarrassing... I am not very familiar with debugging and the terminology. I am doing this as a learning project. Can you tell me how to get the backtrace in qt creator 3.4? I will be happy to provide any information.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Start your app in debugging mode. When it crashes, the backtrace will be presented in the bottom part of the window. You can access "Copy Contents to Clipboard" option from the context menu to copy the result to clipboard so that you can post it here. Other than that you can google gdb+backtrace.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    I am not entirely sure, but I think I found it
    Right click on the left debugger pane -> "create full backtrace"

    Qt Code:
    1. Thread 4 (Thread 0x7fffeb542700 (LWP 2134)):
    2. #0 0x00007ffff5c024ed in poll () from /usr/lib/libc.so.6
    3. No symbol table info available.
    4. #1 0x00007ffff17ef9f2 in ?? () from /usr/lib/libxcb.so.1
    5. No symbol table info available.
    6. #2 0x00007ffff17f156f in xcb_wait_for_event () from /usr/lib/libxcb.so.1
    7. No symbol table info available.
    8. #3 0x00007fffed53d399 in ?? () from /usr/lib/qt/plugins/platforms/libqxcb.so
    9. No symbol table info available.
    10. #4 0x00007ffff6c4c56e in ?? () from /usr/lib/libQt5Core.so.5
    11. No symbol table info available.
    12. #5 0x00007ffff66f7374 in start_thread () from /usr/lib/libpthread.so.0
    13. No symbol table info available.
    14. #6 0x00007ffff5c0b27d in clone () from /usr/lib/libc.so.6
    15. No symbol table info available.
    16.  
    17. Thread 3 (Thread 0x7fffeab36700 (LWP 2135)):
    18. #0 0x00007ffff5c024ed in poll () from /usr/lib/libc.so.6
    19. No symbol table info available.
    20. #1 0x00007ffff3e5cc7c in ?? () from /usr/lib/libglib-2.0.so.0
    21. No symbol table info available.
    22. #2 0x00007ffff3e5cd8c in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
    23. No symbol table info available.
    24. #3 0x00007ffff6e901b7 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    25. No symbol table info available.
    26. #4 0x00007ffff6e35852 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    27. No symbol table info available.
    28. #5 0x00007ffff6c475d4 in QThread::exec() () from /usr/lib/libQt5Core.so.5
    29. No symbol table info available.
    30. #6 0x00007ffff73517d5 in ?? () from /usr/lib/libQt5Qml.so.5
    31. No symbol table info available.
    32. #7 0x00007ffff6c4c56e in ?? () from /usr/lib/libQt5Core.so.5
    33. No symbol table info available.
    34. #8 0x00007ffff66f7374 in start_thread () from /usr/lib/libpthread.so.0
    35. No symbol table info available.
    36. #9 0x00007ffff5c0b27d in clone () from /usr/lib/libc.so.6
    37. No symbol table info available.
    38.  
    39. Thread 2 (Thread 0x7fffe9a84700 (LWP 2136)):
    40. #0 0x00007ffff5c024ed in poll () from /usr/lib/libc.so.6
    41. No symbol table info available.
    42. #1 0x00007ffff3e5cc7c in ?? () from /usr/lib/libglib-2.0.so.0
    43. No symbol table info available.
    44. #2 0x00007ffff3e5cd8c in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
    45. No symbol table info available.
    46. #3 0x00007ffff6e901cc in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    47. No symbol table info available.
    48. #4 0x00007ffff6e35852 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    49. No symbol table info available.
    50. #5 0x00007ffff6c475d4 in QThread::exec() () from /usr/lib/libQt5Core.so.5
    51. No symbol table info available.
    52. #6 0x00007ffff7370948 in ?? () from /usr/lib/libQt5Qml.so.5
    53. No symbol table info available.
    54. #7 0x00007ffff6c4c56e in ?? () from /usr/lib/libQt5Core.so.5
    55. No symbol table info available.
    56. #8 0x00007ffff66f7374 in start_thread () from /usr/lib/libpthread.so.0
    57. No symbol table info available.
    58. #9 0x00007ffff5c0b27d in clone () from /usr/lib/libc.so.6
    59. No symbol table info available.
    60.  
    61. Thread 1 (Thread 0x7ffff7e5c7c0 (LWP 2130)):
    62. #0 0x00007ffff728a81f in QV4::QObjectWrapper::wrap(QV4::ExecutionEngine*, QObject*) () from /usr/lib/libQt5Qml.so.5
    63. No symbol table info available.
    64. #1 0x00007ffff735352c in QV8Engine::fromVariant(QVariant const&) () from /usr/lib/libQt5Qml.so.5
    65. No symbol table info available.
    66. #2 0x00007ffff728f247 in ?? () from /usr/lib/libQt5Qml.so.5
    67. No symbol table info available.
    68. #3 0x00007ffff72904dc in ?? () from /usr/lib/libQt5Qml.so.5
    69. No symbol table info available.
    70. #4 0x00007ffff7290b43 in QV4::QObjectMethod::callInternal(QV4::CallData*) () from /usr/lib/libQt5Qml.so.5
    71. No symbol table info available.
    72. #5 0x00007ffff729c4f8 in QV4::Runtime::callProperty(QV4::ExecutionContext*, QV4::String*, QV4::CallData*) () from /usr/lib/libQt5Qml.so.5
    73. No symbol table info available.
    74. #6 0x00007ffff729620d in ?? () from /usr/lib/libQt5Qml.so.5
    75. No symbol table info available.
    76. #7 0x00007ffff7297056 in ?? () from /usr/lib/libQt5Qml.so.5
    77. No symbol table info available.
    78. #8 0x00007ffff724c2fd in ?? () from /usr/lib/libQt5Qml.so.5
    79. No symbol table info available.
    80. #9 0x00007ffff733754f in ?? () from /usr/lib/libQt5Qml.so.5
    81. No symbol table info available.
    82. #10 0x00007ffff72d8c96 in QQmlBoundSignalExpression::evaluate(void**) () from /usr/lib/libQt5Qml.so.5
    83. No symbol table info available.
    84. #11 0x00007ffff72d9293 in ?? () from /usr/lib/libQt5Qml.so.5
    85. No symbol table info available.
    86. #12 0x00007ffff7319fb7 in QQmlNotifier::emitNotify(QQmlNotifierEndpoint*, void**) () from /usr/lib/libQt5Qml.so.5
    87. No symbol table info available.
    88. #13 0x00007ffff72b739c in QQmlData::signalEmitted(QAbstractDeclarativeData*, QObject*, int, void**) () from /usr/lib/libQt5Qml.so.5
    89. No symbol table info available.
    90. #14 0x00007ffff6e67b41 in QMetaObject::activate(QObject*, int, int, void**) () from /usr/lib/libQt5Core.so.5
    91. No symbol table info available.
    92. #15 0x00007ffff7ccd012 in QQuickMouseArea::clicked(QQuickMouseEvent*) () from /usr/lib/libQt5Quick.so.5
    93. No symbol table info available.
    94. #16 0x00007ffff7c03a76 in QQuickMouseArea::setPressed(Qt::MouseButton, bool) () from /usr/lib/libQt5Quick.so.5
    95. No symbol table info available.
    96. #17 0x00007ffff7c0459b in QQuickMouseArea::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt5Quick.so.5
    97. No symbol table info available.
    98. #18 0x00007ffff7b82028 in QQuickItem::event(QEvent*) () from /usr/lib/libQt5Quick.so.5
    99. No symbol table info available.
    100. #19 0x00007ffff6e37c85 in QCoreApplication::notify(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    101. No symbol table info available.
    102. #20 0x00007ffff6e37dbb in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    103. No symbol table info available.
    104. #21 0x00007ffff7b92753 in QQuickWindow::sendEvent(QQuickItem*, QEvent*) () from /usr/lib/libQt5Quick.so.5
    105. No symbol table info available.
    106. #22 0x00007ffff7b92b22 in QQuickWindowPrivate::deliverMouseEvent(QMouseEvent*) () from /usr/lib/libQt5Quick.so.5
    107. No symbol table info available.
    108. #23 0x00007ffff7b95546 in QQuickWindow::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt5Quick.so.5
    109. No symbol table info available.
    110. #24 0x00007ffff759df25 in QWindow::event(QEvent*) () from /usr/lib/libQt5Gui.so.5
    111. No symbol table info available.
    112. #25 0x00007ffff7b9a1b3 in QQuickWindow::event(QEvent*) () from /usr/lib/libQt5Quick.so.5
    113. No symbol table info available.
    114. #26 0x00007ffff6e37c85 in QCoreApplication::notify(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    115. No symbol table info available.
    116. #27 0x00007ffff6e37dbb in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    117. No symbol table info available.
    118. #28 0x00007ffff7595752 in QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) () from /usr/lib/libQt5Gui.so.5
    119. No symbol table info available.
    120. #29 0x00007ffff7597305 in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) () from /usr/lib/libQt5Gui.so.5
    121. No symbol table info available.
    122. #30 0x00007ffff757c1ef in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Gui.so.5
    123. No symbol table info available.
    124. #31 0x00007fffed5671b0 in ?? () from /usr/lib/qt/plugins/platforms/libqxcb.so
    125. No symbol table info available.
    126. #32 0x00007ffff3e5c9fd in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
    127. No symbol table info available.
    128. #33 0x00007ffff3e5cce0 in ?? () from /usr/lib/libglib-2.0.so.0
    129. No symbol table info available.
    130. #34 0x00007ffff3e5cd8c in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
    131. No symbol table info available.
    132. #35 0x00007ffff6e901b7 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    133. No symbol table info available.
    134. #36 0x00007ffff6e35852 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
    135. No symbol table info available.
    136. #37 0x00007ffff6e3d22c in QCoreApplication::exec() () from /usr/lib/libQt5Core.so.5
    137. No symbol table info available.
    138. #38 0x0000000000403fec in main (argc=1, argv=0x7fffffffe6e8) at ../Renamer4Qml/main.cpp:19
    139. app = <incomplete type>
    140. engine = <incomplete type>
    To copy to clipboard, switch view to plain text mode 

    Start your app in debugging mode. When it crashes, the backtrace will be presented in the bottom part of the window. You can access "Copy Contents to Clipboard" option from the context menu to copy the result to clipboard so that you can post it here. Other than that you can google gdb+backtrace.
    At the bottom I have the 6 buttons
    - Issues
    - Search Results
    - Application Output
    - Compile Output
    - QML/JS Console
    - General Messages
    None of them contains any useful information. When the crash happens the it just stops and I get a disassembly output window.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Quote Originally Posted by motaito View Post
    I am not entirely sure, but I think I found it
    Right click on the left debugger pane -> "create full backtrace"
    It looks like the crash takes place while trying to wrap your QObject instance. This might happen if the pointer points to an invalid object. Did you initialize contents of mData with allocated objects?

    At the bottom I have the 6 buttons
    - Issues
    - Search Results
    - Application Output
    - Compile Output
    - QML/JS Console
    - General Messages
    None of them contains any useful information. When the crash happens the it just stops and I get a disassembly output window.
    Too much to the bottom.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    I doubt that the contents of m_Entries (in my first snippet I called it m_Data) are faulty. Because if they were, it should always fail. Not just sometimes. I have a tableView in a qml which contains a MouseArea in a column. If I press on the MouseArea I would like to perform some tasks. Normally that works but for a entry where it worked before, If I click a bunch of times on the MouseArea suddenly I get a segfault. I have reduced it to only getting the element and not actually doing something with it. There, I get the segfault, if I click several times on the MouseArea.

    My data class looks like this:
    Qt Code:
    1. // header:
    2. #ifndef FILEENTRY_H
    3. #define FILEENTRY_H
    4.  
    5. #include <QObject>
    6.  
    7. class FileEntry : public QObject
    8. {
    9. Q_OBJECT
    10. Q_PROPERTY(QString fileName READ getFileName WRITE setFileName NOTIFY fileNameChanged)
    11. Q_PROPERTY(QString filePath READ getFilePath WRITE setFilePath NOTIFY filePathChanged)
    12.  
    13. public:
    14. FileEntry(QObject* parent=0);
    15.  
    16. void setFileName(const QString& fileName);
    17. void setFilePath(const QString& filePath);
    18. QString getFileName() const;
    19. QString getFilePath() const;
    20.  
    21. signals:
    22. void fileNameChanged(const QString& fileName);
    23. void filePathChanged(const QString& filePath);
    24.  
    25. private:
    26. QString m_FileName;
    27. QString m_FilePath;
    28. };
    29.  
    30. #endif // FILEENTRY_H
    31.  
    32.  
    33. // cpp file:
    34. #include "fileentry.h"
    35.  
    36. FileEntry::FileEntry(QObject* parent)
    37. : QObject(parent)
    38. {
    39. m_FileName = "";
    40. m_FilePath = "";
    41. }
    42.  
    43. void FileEntry::setFileName(const QString& fileName)
    44. {
    45. m_FileName = fileName;
    46. }
    47.  
    48. void FileEntry::setFilePath(const QString& filePath)
    49. {
    50. m_FilePath = filePath;
    51. }
    52.  
    53. QString FileEntry::getFileName() const
    54. {
    55. return m_FileName;
    56. }
    57.  
    58. QString FileEntry::getFilePath() const
    59. {
    60. return m_FilePath;
    61. }
    To copy to clipboard, switch view to plain text mode 
    In my file model I fill the entries like this:
    Qt Code:
    1. bool FileModel::getFiles(const QString& folderPath, const bool append)
    2. {
    3. QDir dir(folderPath);
    4. if(!dir.exists())
    5. return false;
    6.  
    7. if(!append)
    8. {
    9. for(int e=0; e<m_Entries.count(); ++e)
    10. delete m_Entries[e];
    11.  
    12. m_Entries.clear();
    13. }
    14.  
    15. QFileInfoList fileInfoList = dir.entryInfoList();
    16. for(int i=0; i < fileInfoList.count(); ++i)
    17. {
    18. if(fileInfoList[i].isFile())
    19. {
    20. FileEntry* entry = new FileEntry();
    21. entry->setFileName(fileInfoList[i].fileName());
    22. entry->setFilePath(fileInfoList[i].absoluteFilePath());
    23. addEntry(entry);
    24. }
    25. }
    26.  
    27. return true;
    28. }
    29. // getting the data
    30. Q_FileEntry* FileModel::getElement(int index)
    31. {
    32. if(0 > index || index >= m_Entries.size())
    33. return 0;
    34.  
    35. return m_Entries[index];
    36. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    Can we see code executed when you click on MouseArea?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    These are the relevant parts. The area you asked for is towards the bottom.
    Qt Code:
    1. // In main.cpp
    2. qmlRegisterType<FileEntry>("MoeFileEntry", 1, 0, "FileEntryHandler");
    3. qmlRegisterType<FileModel>("MoeFileModel", 1, 0, "FileModelHandler");
    4.  
    5.  
    6. // in qml file
    7. import QtQuick 2.4
    8. import QtQuick.Window 2.2
    9. import QtQuick.Controls 1.2
    10. import QtQuick.Controls.Styles 1.2
    11. import MoeFileEntry 1.0
    12. import MoeFileModel 1.0
    13.  
    14.  
    15. Rectangle {
    16. id: wrapper
    17. anchors.fill: parent
    18.  
    19. FileModelHandler {
    20. id: fileModelHandler
    21. }
    22.  
    23. Component.onCompleted: {
    24. loadFolder("SampleFiles", false);
    25. }
    26.  
    27.  
    28. function loadFolder(directory, append)
    29. {
    30. var path = mediaManagerHandler.getDirectoryPathAbsolute(directory);
    31. fileModelHandler.getFiles(path, append);
    32. }
    33.  
    34.  
    35. TableView {
    36. property int sizeOpen: 60
    37. property int sizeClosed: 20
    38. id: moeTableView
    39. anchors.fill: parent
    40. model: fileModelHandler
    41.  
    42. rowDelegate: Rectangle {
    43.  
    44. id: rowDelegate
    45. color: styleData.alternate ? "#666666" : "#555555"
    46. height: getSize() // styleData.selected? sizeOpen : sizeClosed
    47.  
    48. function getSize()
    49. {
    50. if(!moeTableView.selection.contains(styleData.row))
    51. {
    52. doClose.start();
    53. return moeTableView.sizeClosed;
    54. }
    55.  
    56. return moeTableView.sizeOpen;
    57. }
    58.  
    59. MouseArea {
    60. anchors.top: parent.top
    61. anchors.left: parent.left
    62. anchors.right: parent.right
    63. height: moeTableView.sizeClosed
    64. propagateComposedEvents: true
    65. preventStealing: true
    66. acceptedButtons: Qt.LeftButton | Qt.RightButton
    67.  
    68. onClicked: {
    69. if(moeTableView.sizeOpen == rowDelegate.height)
    70. {
    71. moeTableView.selection.deselect(styleData.row);
    72. doClose.start()
    73. }
    74. else
    75. {
    76. moeTableView.selection.clear();
    77. moeTableView.selection.select(styleData.row);
    78. doOpen.start();
    79. }
    80. }
    81. }
    82.  
    83. ParallelAnimation {
    84. id: doOpen
    85. running: false
    86. NumberAnimation { target: rowDelegate; easing.type: Easing.OutSine; property: "height"; to: moeTableView.sizeOpen; duration: 100 }
    87. }
    88. ParallelAnimation {
    89. id: doClose
    90. running: false
    91. NumberAnimation { target: rowDelegate; easing.type: Easing.OutSine; property: "height"; to: moeTableView.sizeClosed; duration: 100; }
    92. }
    93. }
    94.  
    95. TableViewColumn {
    96. id: icon
    97. width: 50
    98. delegate: Item{
    99. anchors.fill: parent
    100. clip: !styleData.selected
    101. Rectangle {
    102. clip:false
    103. anchors.top: parent.top
    104. anchors.bottom: parent.bottom
    105. anchors.left: parent.left
    106. width: moeTableView.width
    107. color: "#222222"
    108. }
    109. Rectangle {
    110. width:20
    111. height:20
    112. color:"red"
    113. anchors.top: parent.top
    114. anchors.left: parent.left
    115.  
    116. MouseArea {
    117. anchors.fill:parent
    118. onClicked: {
    119. // Here is the problem area where the segfault occurs!
    120. // I am not useing "e" , I can also not assign it and
    121. // just call the function, the segfault still happens.
    122. var e = fileModelHandler.getElement(styleData.row);
    123. }
    124. }
    125. }
    126. }
    127. }
    128.  
    129. TableViewColumn {
    130. id: rowFileName
    131. role: "fileName"
    132. title: "Name"
    133. width: 100
    134. delegate: Item {
    135. id: itemFileName
    136. clip:true
    137. anchors.fill: parent
    138. Text{
    139. id:txtFileName
    140. anchors.fill: parent
    141. text: styleData.value
    142. elide: Text.ElideRight
    143. }
    144. }
    145. }
    146.  
    147. TableViewColumn {
    148. id: rowFilePath
    149. role: "filePath"
    150. title: "Path"
    151. width: 200
    152. delegate: Item {
    153. id: itemFilePath
    154. anchors.fill: parent
    155. clip: true
    156. Text{
    157. id:txtFilePath
    158. anchors.top: parent.top
    159. anchors.left: parent.left
    160. anchors.right: parent.right
    161. text: styleData.value
    162. elide: Text.ElideRight
    163. }
    164. }
    165. }
    166.  
    167. }
    168. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    I don't see anything suspicious here. I would still opt for an invalid pointer in the list. You need to use the debugger to check it out.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    You need to set the ownership to CppOwnership of the object before returning it from the Q_INVOKABLE method.
    If you do not, then Qt assumes JavaScriptOwnerShip and garbage collects the object, at which point the pointer in your list points to an invalid address.

    Cheers,
    _

  12. #12
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.4 sudden segfault when calling invokable c++ function from qml

    I don't see anything suspicious here. I would still opt for an invalid pointer in the list. You need to use the debugger to check it out.
    I am not sure how to do that because it only happens after clicking several times in a succession. If I make a break point, I don't really get to the needed amount of clicks. I guess I could add some sort of assert check in the getElement(index) function. I'll research how to check for a valid pointer.

    You need to set the ownership to CppOwnership of the object before returning it from the Q_INVOKABLE method.
    If you do not, then Qt assumes JavaScriptOwnerShip and garbage collects the object, at which point the pointer in your list points to an invalid address.
    That sounds very reasonable. I have not thought of that at all. It would explain why the crash happens suddenly and not right away. It would take some time for the garbage collection to kick in. I am however not sure how to set the ownership because it needs to be set per object. In my case I create the objects in my FileModel class. An instance of the FileModel class is created within a qml file. So I can't figure out how to get a hold of the proper context from the QQmlApplicationEngine. How would I set the object ownership?


    Added after 56 minutes:


    Never mind I found I can use
    Qt Code:
    1. FileEntry* entry = new FileEntry();
    2. QQmlEngine::setObjectOwnership(entry, QQmlEngine::CppOwnership);
    To copy to clipboard, switch view to plain text mode 
    without having to have a handle on the QQmlApplicationEngine. I still have a hard time to make heads and tails of the documentation

    The problem has not happened since I use it this way. So, it looks like you guys were both right. The problem was caused by an invalid pointer. It got invalidated by the javascript engine destroying the object through the garbage collection. Once again you guys saved my bacon

    Thanks a lot for the help and the insight in what is happening!
    Last edited by motaito; 26th April 2015 at 13:40.

Similar Threads

  1. calling a function with objects
    By herculis in forum Newbie
    Replies: 3
    Last Post: 28th August 2014, 10:36
  2. Calling external DLL function with Qt
    By yaba in forum Newbie
    Replies: 2
    Last Post: 29th September 2011, 07:47
  3. function returns QString, usage generates segfault
    By space_otter in forum General Programming
    Replies: 5
    Last Post: 8th March 2010, 15:54
  4. QtConncurrent - calling function within the class
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2009, 17:37
  5. Replies: 3
    Last Post: 26th October 2009, 19:11

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.