4

The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache. How can I insert a shared pointer to QCache? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it? Will the memory be released and my cache points to nowhere?

Orest Hera
  • 6,706
  • 2
  • 21
  • 35
adapto
  • 95
  • 1
  • 12
  • 1
    Shared pointers use reference counting, so nothing will get deleted by QSharedPointer until the last QSharedPointer pointing to it is destroyed. – sashoalm Oct 22 '15 at 12:25

1 Answers1

4

It is a bug if you put just a pointer to your item to QChache and at the same time such pointer is managed by QSharedPointer. The item object can be destroyed by QSharedPointer destructor, so QChache will have invalid pointer. It is a generic issue that you cannot have different owners of a pointer that do not know each other. If you manage pointers by QSharedPointer<Item> then QChache should not work directly with Item it should work only with QSharedPointer<Item>, for example:

// wrong
QCache<int, Item> cache;
QVector<QSharedPointer<Item> > vec;
Item *item = new Item;
vec.push_back(QSharedPointer<Item>(item));
cache.insert(1, item);
// or
cache.insert(1, vec.at(0).data());

// also wrong
QCache<int, <QSharedPointer<Item> > cache;
vec.push_back(QSharedPointer<Item>(item));
cache.insert(1, new QSharedPointer<Item>(item));
// here two different instances of QSharedPointer are initialized
// by the same pointer 'item'

// correct
QCache<int, <QSharedPointer<Item> > cache;
cache.insert(1, new QSharedPointer<Item>(vec.at(0)));

So, QCache should have its own copy of dynamically allocated QSharedPointer object and that QSharedPointer object should be correctly initialized by other QSharedPointer that already has ownership of the Item instance.

Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • Thanks I will try this. But in another thread someone told me not to use smart pointers with QCache. See here: http://stackoverflow.com/a/33158035/5447060 – adapto Oct 22 '15 at 14:55
  • 1
    @adapto That is because you did not mention in your question that the same object will be used outside of `QCache`. It is ok if you allocate a new object and then pass it to `QCache`, but in that case that pointer should not be owned by any other object. – Orest Hera Oct 22 '15 at 15:06
  • But in this case no need for shared_ptr exists in QCache. OP can simply do this: `cache.insert(1, new Item(item));` – SergeyA Oct 22 '15 at 15:26
  • @SergeyA OP wants to keep those pointers in external vector, but `QCache` can delete items when it decides to make free place during insertion of new items, so pointers in that vector become invalid. – Orest Hera Oct 22 '15 at 15:30
  • Right. So this is why there is no need to create another shared_ptr only to put it into the cache. Cache is a collection of shared_ptrs already, it manages them. Just an extra indirection. – SergeyA Oct 22 '15 at 15:58
  • 2
    Unfortunately `QCache` is rather container of unique_ptrs. So to share that pointer with external shared_prts there is no other solution than wrapping `QCache` managed item by shared_prt. – Orest Hera Oct 22 '15 at 17:29