diff --git a/CHANGELOG.md b/CHANGELOG.md index a40bcbb4..38bae762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561) + v5.11.1 ------- diff --git a/src/ArduinoJson/DynamicJsonBuffer.hpp b/src/ArduinoJson/DynamicJsonBuffer.hpp index 77da72cc..65e744bf 100644 --- a/src/ArduinoJson/DynamicJsonBuffer.hpp +++ b/src/ArduinoJson/DynamicJsonBuffer.hpp @@ -52,7 +52,7 @@ class DynamicJsonBufferBase : _head(NULL), _nextBlockCapacity(initialSize) {} ~DynamicJsonBufferBase() { - freeAllBlocks(); + clear(); } // Gets the number of bytes occupied in the buffer @@ -71,7 +71,13 @@ class DynamicJsonBufferBase // Resets the buffer. // USE WITH CAUTION: this invalidates all previously allocated data void clear() { - freeAllBlocks(); + Block* currentBlock = _head; + while (currentBlock != NULL) { + _nextBlockCapacity = currentBlock->capacity; + Block* nextBlock = currentBlock->next; + _allocator.deallocate(currentBlock); + currentBlock = nextBlock; + } _head = 0; } @@ -144,16 +150,6 @@ class DynamicJsonBufferBase return true; } - void freeAllBlocks() { - Block* currentBlock = _head; - - while (currentBlock != NULL) { - Block* nextBlock = currentBlock->next; - _allocator.deallocate(currentBlock); - currentBlock = nextBlock; - } - } - TAllocator _allocator; Block* _head; size_t _nextBlockCapacity; diff --git a/test/DynamicJsonBuffer/alloc.cpp b/test/DynamicJsonBuffer/alloc.cpp index a821e386..091fc786 100644 --- a/test/DynamicJsonBuffer/alloc.cpp +++ b/test/DynamicJsonBuffer/alloc.cpp @@ -46,7 +46,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") { REQUIRE(allocatorLog.str() == "A1A2FF"); } - SECTION("Keeps increasing allocation size after clear") { + SECTION("Resets allocation size after clear()") { allocatorLog.str(""); { DynamicJsonBufferBase buffer(1); @@ -55,7 +55,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") { buffer.clear(); buffer.alloc(1); } - REQUIRE(allocatorLog.str() == "A1A2FFA4F"); + REQUIRE(allocatorLog.str() == "A1A2FFA1F"); } SECTION("Makes a big allocation when needed") {