Giter Club home page Giter Club logo

Comments (6)

bblanchon avatar bblanchon commented on May 22, 2024

It's true that DynamicJsonBuffer uses dynamic allocation for its content.

But the class itself is not intended to be dynamically allocated.
Indeed it's designed to avoid that.

You never write new DynamicJsonBuffer().
So you never write delete myJsonBuffer.
So the destructor is always statically called.
Therefore it's still not relevant to make it virtual.

And anyway, I bet that if you make it virtual without changing the base class, you'll trigger another warning.

I don't manage to reproduce the warning on older version of the compiler.
Can you try to add a pragma?

from arduinojson.

bblanchon avatar bblanchon commented on May 22, 2024

I'm sorry I tested on many compilers, including GCC 4.9, but I cannot reproduce this warning.

from arduinojson.

CZEMacLeod avatar CZEMacLeod commented on May 22, 2024

@bblanchon I am getting this issue, and I don't know that I agree with the assertion that you never new the JsonBuffer.
In my case, I am allocating the buffer and storing the pointer and building up a message, perhaps over several function calls or loops.
Once the message is built and written, the buffer needs to be 'released' as you explicitly say don't share or re-use the buffer.
Thus, after saving the json message, I delete the buffer and set the pointer to nullptr.
Before building a new json object

	if (jsonBuffer == nullptr) {
		jsonBuffer = new DynamicJsonBuffer();
	}

Build the object root

        JsonObject & root = (*jsonBuffer).createObject();

Later on after finishing with the object root

	if (jsonBuffer != nullptr) {
		jsonBuffer->clear();
		delete jsonBuffer;
		jsonBuffer = nullptr;
	}

Note that this is done in a class where there can be multiple instances.
Maybe this isn't the best approach, but it certainly seems to work as expected and fulfills my requirements, but if there is another/better way to deal with this I will certainly look at it.
I don't have any memory leaks or other issues and have plenty of space to work with when using a Cortex M4+ type device.

I am not sure the reason you don't have virtual destructors but I found that the compiler warnings (and most documentation say they are actually rather more than warnings) go away if you make the base class have a virtual destructor.
In JsonBufferBase.hpp

 protected:
  virtual ~JsonBufferBase() {}

from arduinojson.

bblanchon avatar bblanchon commented on May 22, 2024

Hi @CZEMacLeod,

Why do you use new and delete?

from arduinojson.

CZEMacLeod avatar CZEMacLeod commented on May 22, 2024

As I explained in the previous message, I have a process that runs through a number of stages, and adds data to the json message at each stage. Once the message is complete (after many times through loop() and calling to different subroutines) it gets saved to SD and sent and then a new message is started.

The scope of the buffer and root object are global, not local and need to be deleted at the end of each message and newed at the beginning of the next.

from arduinojson.

bblanchon avatar bblanchon commented on May 22, 2024

I strongly recommend making the buffer local to the loop.
If you believe that this is impossible, just call JsonBuffer::clear()

Regards,
Benoit

from arduinojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.