Skip to content

Commit 6c55f07

Browse files
committed
Add "struct Xxx has no member named reallocate"
See bblanchon/ArduinoJson#2118
1 parent 28f2fd3 commit 6c55f07

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/pages/v7/compiletime/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ options:
3535
summary: Error says "`class Xxx` has no member named `read`"
3636
page: no-member-named-read.md
3737

38+
class-has-no-member-named-reallocate:
39+
label: "`class Xxx` has no member named `reallocate`; did you mean `deallocate`?"
40+
summary: Error says "`class Xxx` has no member named `reallocate`"
41+
page: no-member-named-reallocate/index.md
42+
3843
pointer-to-object:
3944
label: '`const void*` is not a pointer-to-object type'
4045
summary: Error says "`const void*` is not a pointer-to-object type"
@@ -105,6 +110,11 @@ options:
105110
summary: Error says "`struct Xxx` has no member named `read`"
106111
page: no-member-named-read.md
107112

113+
struct-has-no-member-named-reallocate:
114+
label: "`struct Xxx` has no member named `reallocate`; did you mean `deallocate`?"
115+
summary: Error says "`struct Xxx` has no member named `reallocate`"
116+
page: no-member-named-reallocate/index.md
117+
108118
range-based-for-requires-begin:
109119
label: this range-based `for` statement requires a suitable "begin" function and none was found
110120
summary: Error says "this range-based `for` statement requires a suitable "begin" function and none was found"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
options:
3+
upgrade:
4+
label: Upgrade to ArduinoJson 7 syntax (recommended)
5+
summary: User wants to upgrade to ArduinoJson 7 syntax
6+
page: upgrade.md
7+
failure:
8+
label: Keep to ArduinoJson 6 syntax (quick fix)
9+
summary: User wants to keep to ArduinoJson 6 syntax
10+
page: keep.md
11+
---
12+
13+
This error occurs when the following condition are met:
14+
15+
1. The program was designed for ArduinoJson 6.
16+
2. The program uses a custom allocator.
17+
3. The custom allocator doesn't define `reallocate()`.
18+
19+
Indeed, in ArduinoJson 6, the `reallocate()` method was optional for custom allocators.
20+
It's now mandatory in ArduinoJson 7, and that's why you get this error.
21+
22+
There are two ways to fix this issue. You can either:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
options:
3+
success:
4+
label: "Yes"
5+
summary: Adding the `reallocate()` method fixes the issue
6+
page: /done.md
7+
failure:
8+
label: "No"
9+
summary: Adding the `reallocate()` method doesn't fix the issue
10+
page: /deadend.md
11+
---
12+
13+
You must add the `reallocate()` method like so:
14+
15+
```diff
16+
struct SpiRamAllocator {
17+
void* allocate(size_t size) {
18+
return heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
19+
}
20+
21+
void deallocate(void* pointer) {
22+
heap_caps_free(pointer);
23+
}
24+
25+
+ void* reallocate(void* ptr, size_t new_size) {
26+
+ return heap_caps_realloc(ptr, new_size, MALLOC_CAP_SPIRAM);
27+
+ }
28+
};
29+
```
30+
31+
Did this solve your issue?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
options:
3+
success:
4+
label: "Yes"
5+
summary: Upgrading the syntax and adding the `reallocate()` method fixes the issue
6+
page: /done.md
7+
failure:
8+
label: "No"
9+
summary: Upgrading the syntax and adding the `reallocate()` method doesn't fix the issue
10+
page: /deadend.md
11+
---
12+
13+
Here is how to upgrade your code:
14+
15+
```diff
16+
- struct SpiRamAllocator {
17+
+ struct SpiRamAllocator : ArduinoJson::Allocator {
18+
void* allocate(size_t size) {
19+
return heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
20+
}
21+
22+
void deallocate(void* pointer) {
23+
heap_caps_free(pointer);
24+
}
25+
26+
+ void* reallocate(void* ptr, size_t new_size) {
27+
+ return heap_caps_realloc(ptr, new_size, MALLOC_CAP_SPIRAM);
28+
+ }
29+
};
30+
31+
- using SpiRamJsonDocument = BasicJsonDocument<SpiRamAllocator>;
32+
+ SpiRamAllocator allocator;
33+
34+
- SpiRamJsonDocument doc;
35+
+ JsonDocument doc(&allocator);
36+
```
37+
38+
For more information about the changes in ArduinoJson 7, please refer to the [upgrade guide](/v7/how-to/upgrade-from-v6/).
39+
40+
Did this solve your issue?

0 commit comments

Comments
 (0)