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?
相关问题
- Sorting 3 numbers without branching [closed]
- QML: Cannot read property 'xxx' of undefin
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
It is a bug if you put just a pointer to your item to
QChache
and at the same time such pointer is managed byQSharedPointer
. The item object can be destroyed byQSharedPointer
destructor, soQChache
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 byQSharedPointer<Item>
thenQChache
should not work directly withItem
it should work only withQSharedPointer<Item>
, for example:So,
QCache
should have its own copy of dynamically allocatedQSharedPointer
object and thatQSharedPointer
object should be correctly initialized by otherQSharedPointer
that already has ownership of theItem
instance.