Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConcurrent: function object with qhash as member
Forum Update on Monday, May 27th 2025

QtConcurrent: function object with qhash as member

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 472 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.
  • J Offline
    J Offline
    JanW
    wrote on 14 Mar 2019, 07:18 last edited by
    #1

    Hi,

    I'm having some trouble with the QtConcurrent api. When I try to use a function object in the QtConcurrent::blockingMap function which contains a QHash as a member variable, my application crashes when i try to look up something in that qhash in the parallell call. My (little bit simplified) code looks like this:

    struct ParentChildFunctionObject
    {
    	ParentChildFunctionObject(QHash<QUuid, QString> lookupHash)
    		: _lookupHash(lookupHash)
    	{}
    	void operator()(const QUuid& guid)
    	{
              //...
              QHash<QUuid, QString>::iterator it = _lookupHash.find(guid)  //->  crash here
              //...
    	}
    	QHash<QUuid, QString> _lookupHash
    };
    
    void ImportOperation::fillParentChildStructure()
    {
    	QtConcurrent::blockingMap(_myListOfQUuids, ParentChildFunctionObject(_myLookupHash));
            //_myListOfQUuids is a QVector<QUuid> and _myLookupHash is a QHash<QUuid, QString>
    }
    

    The crash occurs in the find method of the qhash (when I just assign a fixed value to the string i need, everything works fine).

    What am I missing?
    Thanks,

    Jan

    Ps: An alternative would be to give the string I need as an extra parameter in the blockingMap parallel called function. Is that possible?

    Pps: Qt 5.12, msvc2017 compiler, Win10, but i don't think it matters :)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Mar 2019, 11:17 last edited by
      #2

      Since find() is done on a non-const QHash the first thing which is done is detach(). Since detach() itself is not thread-safe you will maybe get a crash here due to concurrency.
      When you don't modify the QHash I would pass it as const reference (this should be done in any case to avoid a copy) and store it as 'const QHash<QUuid, QString> _lookupHash' or even as 'QHash<QUuid, QString> &_lookupHash' so find() will work on a const container so it doesn't get detached.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply 14 Mar 2019, 12:10
      3
      • C Christian Ehrlicher
        14 Mar 2019, 11:17

        Since find() is done on a non-const QHash the first thing which is done is detach(). Since detach() itself is not thread-safe you will maybe get a crash here due to concurrency.
        When you don't modify the QHash I would pass it as const reference (this should be done in any case to avoid a copy) and store it as 'const QHash<QUuid, QString> _lookupHash' or even as 'QHash<QUuid, QString> &_lookupHash' so find() will work on a const container so it doesn't get detached.

        J Offline
        J Offline
        JanW
        wrote on 14 Mar 2019, 12:10 last edited by
        #3

        That indeed seems to be the problem. After changing to const ref, it doesn't crash anymore.
        Thanks!

        Jan

        1 Reply Last reply
        0

        1/3

        14 Mar 2019, 07:18

        • Login

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