diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-23 14:08:33 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-27 20:57:49 +0200 |
commit | b095d268788343b67a3995db7148dcc3af9bde1a (patch) | |
tree | acdd5081f47e3f055ef1bd145bf01d3149e16bf7 /src | |
parent | c8782acd457a05d366f611eca4f146a1659c91ea (diff) |
QHash/QSet: fix squeeze() for default-constructed container
QHash::squeeze() was unconditionally calling reserve(0), which is
always allocating memory (even for 0 size).
This was leading to a confusing situation when calling squeeze() on
a default-constructed container with 0 capacity() actually allocated
memory. This is very misleading, as squeeze() is supposed to free
unneeded memory, not to allocate more.
This patch adds a check for non-zero capacity. As a result, nothing
is done for default-constructed container.
Note that this patch also affects the QSet::squeeze() behavior, because
QSet uses QHash as its underlying data type.
Task-number: QTBUG-91736
Pick-to: 6.2 6.1
Change-Id: Ib1c3c8b7b3de6ddeefea0e70b1ec71803e8fd3b3
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qhash.h | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index b030fd2a93..38f23d822f 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -850,7 +850,11 @@ public: else d = Data::detached(d, size_t(size)); } - inline void squeeze() { reserve(0); } + inline void squeeze() + { + if (capacity()) + reserve(0); + } inline void detach() { if (!d || d->ref.isShared()) d = Data::detached(d); } inline bool isDetached() const noexcept { return d && !d->ref.isShared(); } |