[cpp] Fixes #1159, illegal memory access in Vector.

This commit is contained in:
badlogic 2018-08-10 11:52:59 +02:00
parent d3e37284fc
commit 1c86d63941

View File

@ -95,12 +95,19 @@ public:
inline void add(const T &inValue) {
if (_size == _capacity) {
// inValue might reference an element in this buffer
// When we reallocate, the reference becomes invalid.
// We thus need to create a defensive copy before
// reallocating.
T valueCopy = inValue;
_capacity = (int) (_size * 1.75f);
if (_capacity < 8) _capacity = 8;
_buffer = Spine::SpineExtension::realloc<T>(_buffer, _capacity, __FILE__, __LINE__);
}
_buffer = spine::SpineExtension::realloc<T>(_buffer, _capacity, __FILE__, __LINE__);
construct(_buffer + _size++, valueCopy);
} else {
construct(_buffer + _size++, inValue);
}
}
inline void addAll(Vector<T> &inValue) {
ensureCapacity(this->size() + inValue.size());