5

I am trying to prevent naked pointers, to prevent memory leaking etc. I also want to map int to INuiSensor*. Since I am also using Qt I tried to use QMap<int, std::unique_ptr<INuiSensor>> to do this, but the source code of QMap makes this impossible:

template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, const T &avalue)
{
    detach();
    Node *n = d->root();
    Node *y = d->end();
    Node *last = 0;
    bool  left = true;
    while (n) {
        y = n;
        if (!qMapLessThanKey(n->key, akey)) {
            last = n;
            left = true;
            n = n->leftNode();
        } else {
            left = false;
            n = n->rightNode();
        }
    }
    if (last && !qMapLessThanKey(akey, last->key)) {
        last->value = avalue;
        return iterator(last);
    }
    Node *z = d->createNode(akey, avalue, y, left);
    return iterator(z);
}

The line:

last->value = avalue;

Is the one that creates the problem: you cannot use the =operator directly on a unique_ptr. So now I am puzzeled on what to do next. Is it possible to use QMap and unique_ptr in some other way? Is the whole idea of using QMap and unique_ptr stupid for some reason? What can I do to prevent using naked pointers while still using a QMap?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Cheiron
  • 3,620
  • 4
  • 32
  • 63
  • 2
    That's because Qt containers and Qt pointers are shit, frankly speaking. Use std::map, especially if you are already using `std::unique_ptr`. Or _misuse_ `QSharedPointer`. – Lol4t0 Apr 28 '13 at 13:26
  • @Lol4t0 That's a strong statement, any evidence to back it up? – cmannett85 Apr 28 '13 at 13:48
  • 1
    @cmannett85, this one, `QScopedPointer` does not support move semantics, Qt containers consider taking an iterator as modification (that's why qvector.begin() is not thread safe), only unordered set, hash function implementation is tricky and you cannot specify one directly and so on. – Lol4t0 Apr 28 '13 at 14:00

2 Answers2

2

using Qt container, you should use Qt smart pointers implementation. More about the different implementations in this thread.

What C++ Smart Pointer Implementations are available?

as mentionned, you could go with QSharedPointer.

Community
  • 1
  • 1
kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • The closest approximation to a `unique_ptr` in Qt would be `QScopedPointer`. But since it doesn't implement move semantics it is useless in all but the simplest use cases. – relgukxilef Sep 19 '19 at 18:32
-1

Of course this isn't going to work, you have created a unique pointer and then you try to copy it into a QMap.

Use a QSharedPointer. When all references to the INuiSensor* instance have been deleted, the instance will be deleted.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • I am not trying to copy it into the qmap, I am std::moving it into the qmap. Problem is that the QMap them copies it into its internal datastructure, which is the actual problem. Thats why I am stating that the problem is in the QMap source code and not in my code(which doesn't mean its not my fault, but at least im smart enough to move a unique_ptr, and not just copy it). – Cheiron Apr 29 '13 at 08:44
  • @Cheiron Then you should have stated that in your question. – cmannett85 Apr 29 '13 at 08:46