interactive-matter / aJson

aJson is an Arduino library to enable JSON processing with Arduino. It easily enables you to decode, create, manipulate and encode JSON directly from and to data structures.
http://interactive-matter.org/2010/08/ajson-handle-json-with-arduino/
566 stars 136 forks source link

Memory Leak in print() function? #39

Open asaeed opened 11 years ago

asaeed commented 11 years ago

I found that when I try to print the JSON string, there's a memory leak. Not sure if it's related to the other issues with memory leak. Try running the following sketch:

#include <aJSON.h>

void setup() {
  Serial.begin(9600);

  freeMem("start 1");
  aJsonObject* root = aJson.createObject();
  aJsonObject *neck;
  aJson.addItemToObject(root, "val",  neck = aJson.createItem(123.45)); 
  Serial.println(aJson.print(root));
  aJson.deleteItem(root);
  freeMem("end 1"); 

  Serial.println();

  freeMem("start 2");
  aJsonObject* root1 = aJson.createObject();
  aJsonObject *neck1;
  aJson.addItemToObject(root1, "val",  neck1 = aJson.createItem("123.45")); 
  Serial.println("{\"val\":123.45}");
  aJson.deleteItem(root1);
  freeMem("end 2"); 
}

void loop() {

}

//Code to print out the free memory

extern unsigned int __heap_start;
extern void *__brkval;

/*
 * The free list structure as maintained by the 
 * avr-libc memory allocation routines.
 */
struct __freelist {
  size_t sz;
  struct __freelist *nx;
};

/* The head of the free list structure */
extern struct __freelist *__flp;

/* Calculates the size of the free list */
int freeListSize() {
  struct __freelist* current;
  int total = 0;

  for (current = __flp; current; current = current->nx) {
    total += 2; /* Add two bytes for the memory block's header  */
    total += (int) current->sz;
  }
  return total;
}

int freeMemory() {
  int free_memory;

  if ((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__heap_start);
  } else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
    free_memory += freeListSize();
  }
  return free_memory;
}

void freeMem(char* message) {
  Serial.print(message);
  Serial.print(": ");
  Serial.println(freeMemory());
}

I'm going to see if i can find a fix but thought I'd post to let you know. If anyone can recommend a clean fix, i'm all ears!

asaeed commented 11 years ago

figured it out, and then noticed it's the very first issue raised on this lib... :| should have read prior issues more carefully.

use

char* json = aJson.print(root);
Serial.println(json);
free(json);

instead of

Serial.println(aJson.print(root));
interactive-matter commented 11 years ago

hmm, keeping this issue half open - there is positvely a flaw in the documentation ;) thanks

Marcus

xmoulin commented 11 years ago

I have the same issue... 15 loop and than Serial.println(json) write nothing... :o/

xmoulin commented 11 years ago

Of course, the operation "free(json);" solve the problem. Thx ;o)

rvdende commented 7 years ago

Thanks asaeed saving my ass here

ryanneve commented 7 years ago

Please update the aJson.print() examples in the README!