8000 Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (fix… · ROMSDEV/ArduinoJson@57defe0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57defe0

Browse files
committed
Fixed DynamicJsonBuffer::clear() not resetting allocation size (fixes bblanchon#561)
1 parent ac5a267 commit 57defe0

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561)
8+
49
v5.11.1
510
-------
611

src/ArduinoJson/DynamicJsonBuffer.hpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DynamicJsonBufferBase
5252
: _head(NULL), _nextBlockCapacity(initialSize) {}
5353

5454
~DynamicJsonBufferBase() {
55-
freeAllBlocks();
55+
clear();
5656
}
5757

5858
// Gets the number of bytes occupied in the buffer
@@ -71,7 +71,13 @@ class DynamicJsonBufferBase
7171
// Resets the buffer.
7272
// USE WITH CAUTION: this invalidates all previously allocated data
7373
void clear() {
74-
freeAllBlocks();
74+
Block* currentBlock = _head;
75+
while (currentBlock != NULL) {
76+
_nextBlockCapacity = currentBlock->capacity;
77+
Block* nextBlock = currentBlock->next;
78+
_allocator.deallocate(currentBlock);
79+
currentBlock = nextBlock;
80+
}
7581
_head = 0;
7682
}
7783

@@ -144,16 +150,6 @@ class DynamicJsonBufferBase
144150
return true;
145151
}
146152

147-
void freeAllBlocks() {
148-
Block* currentBlock = _head;
149-
150-
while (currentBlock != NULL) {
151-
Block* nextBlock = currentBlock->next;
152-
_allocator.deallocate(currentBlock);
153-
currentBlock = nextBlock;
154-
}
155-
}
156-
157153
TAllocator _allocator;
158154
Block* _head;
159155
size_t _nextBlockCapacity;

test/DynamicJsonBuffer/alloc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") {
4646
REQUIRE(allocatorLog.str() == "A1A2FF");
4747
}
4848

49-
SECTION("Keeps increasing allocation size after clear") {
49+
SECTION("Resets allocation size after clear()") {
5050
allocatorLog.str("");
5151
{
5252
DynamicJsonBufferBase<SpyingAllocator> buffer(1);
@@ -55,7 +55,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") {
5555
buffer.clear();
5656
buffer.alloc(1);
5757
}
58-
REQUIRE(allocatorLog.str() == "A1A2FFA4F");
58+
REQUIRE(allocatorLog.str() == "A1A2FFA1F");
5959
}
6060

6161
SECTION("Makes a big allocation when needed") {

0 commit comments

Comments
 (0)
0