Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #561
Closed
Open
Issue created Jul 26, 2017 by Administrator@rootContributor

DynamicJsonBuffer::clear() should reset the allocation size too

Created by: ewaldc

Hi Benoit, Apologies for filing a new question for this really cool library. Pre-5.11, I used StaticJsonBufferBase inside in my class library. This gave me full control of the allocation of the buffer space (stack, heap, flash), the scope in relation to the class objects, ability to deal with JSON strings of dynamic length (unknown at compile time) and the performance of StaticJsonBuffer (a single flat buffer space) etc. A major disadvantage is that I have to get the length of the buffer right (no auto extension) using estimation code and now, since 5.11, the reality that it longer works. Hence, I followed your suggestion to use DynamicJsonBuffer(bufferSize) instead. Recoding was easy, the clear() function allows me to free the memory when the buffer is no longer needed ahead of the disposal of an instantiated class object, performance difference is small, but.. I was unable to solve the scope issue :-(.

The original class definition (shortened): class OpenHab { protected: char *_sitemapBuffer; StaticJsonBufferBase *_sitemapJsonBufferPtr; JsonVariant _siteMap; }

and code

_sitemapBuffer = (char *) malloc (size);  // allocate the buffer
StaticJsonBufferBase sitemapJsonBuffer = StaticJsonBufferBase(_sitemapBuffer, size);
_sitemapJsonBufferPtr = &sitemapJsonBuffer;   // save buffer just in case
_siteMap = sitemapJsonBuffer.parse(sitemapJson, 20);  // increase nestingLimit !

The new class definition (shortened): class OpenHab { protected: DynamicJsonBuffer _sitemapJsonBuffer; JsonVariant _siteMap; }

and parsing code

DynamicJsonBuffer _sitemapJsonBuffer(size);
_siteMap = _sitemapJsonBuffer.parse(sitemapJson, 20);  // increase nestingLimit !

The problem is that _sitemapJsonBuffer seems to go out of scope at the end of the function containing the parsing code. Adding a class initializer e.g. _sitemapJsonBuffer(1) did not solve it either.

I am sure you answered this once before, but I searched for an hour and could not find it.

Platform: NodeMCU 1.0/ESP 12E with ESP8266 board manager 2.3.0 Library: ArduinoJson 5.11.1 IDE: Arduino IDE 18.3

Thanks in advance, Ewald

Assignee
Assign to
Time tracking