Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt Quick Windows app crashes when C++ try to access element (in range) of a vector
Forum Update on Monday, May 27th 2025

Qt Quick Windows app crashes when C++ try to access element (in range) of a vector

Scheduled Pinned Locked Moved Solved QML and Qt Quick
c++qmlcrashvectorqtquick
3 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TommyX
    wrote on 26 Jul 2016, 14:13 last edited by TommyX
    #1

    Hi,

    My Qt Quick application on Windows is experiencing a very weird crash.

    I have a QObject derived c++ class called QObjectVector that contains a QVector<QObject*> m_data to store some objects created on the heap. I have a Q_INVOKABLE QObject* QObjectVector::at(index) function for QML side to access elements in that vector.
    On the QML side, I have a bunch of buttons, each with a property currentObject, and the button text is bound to currentObject.name. when I scroll the mouse wheel, I manually update the currentObject of each button using the at function (the text get updated too by the binding).

    Strangely, and only sometimes, after an unpredictable number of fast scrolling, the program crashes (stopped working). After debugging, I found that the crashing happens when the at function access m_data[i]. I have made sure that i is within the valid range [0, m_data.size()), so it is not an index-out-of-range error. My guess is that, somehow, the QML engine deletes my objects on the heap when trying to manage memory.

    This problem is very weird and unpredictable, and solving it is very crucial to the development of my app, so please please please, any help will be appreciated.

    Thank you!

    Tommy

    ? 1 Reply Last reply 26 Jul 2016, 14:27
    0
    • T TommyX
      26 Jul 2016, 14:13

      Hi,

      My Qt Quick application on Windows is experiencing a very weird crash.

      I have a QObject derived c++ class called QObjectVector that contains a QVector<QObject*> m_data to store some objects created on the heap. I have a Q_INVOKABLE QObject* QObjectVector::at(index) function for QML side to access elements in that vector.
      On the QML side, I have a bunch of buttons, each with a property currentObject, and the button text is bound to currentObject.name. when I scroll the mouse wheel, I manually update the currentObject of each button using the at function (the text get updated too by the binding).

      Strangely, and only sometimes, after an unpredictable number of fast scrolling, the program crashes (stopped working). After debugging, I found that the crashing happens when the at function access m_data[i]. I have made sure that i is within the valid range [0, m_data.size()), so it is not an index-out-of-range error. My guess is that, somehow, the QML engine deletes my objects on the heap when trying to manage memory.

      This problem is very weird and unpredictable, and solving it is very crucial to the development of my app, so please please please, any help will be appreciated.

      Thank you!

      Tommy

      ? Offline
      ? Offline
      A Former User
      wrote on 26 Jul 2016, 14:27 last edited by A Former User
      #2

      @TommyX said:

      My guess is that, somehow, the QML engine deletes my objects on the heap when trying to manage memory.

      You're right. When you return a QObject* from C++ to QML, the default behaviour is that the QML engine takes ownership of the object. You can change this, see: http://doc.qt.io/qt-5/qtqml-cppintegration-data.html and http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership.

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TommyX
        wrote on 26 Jul 2016, 15:19 last edited by
        #3

        @Wieland

        Thank you so much! I added QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); when the object was created, and this apparently solves the problem :)

        1 Reply Last reply
        0

        3/3

        26 Jul 2016, 15:19

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved