{"action":"created","issue":{"url":"https://api.github.com/repos/SmingHub/Sming/issues/378","labels_url":"https://api.github.com/repos/SmingHub/Sming/issues/378/labels{/name}","comments_url":"https://api.github.com/repos/SmingHub/Sming/issues/378/comments","events_url":"https://api.github.com/repos/SmingHub/Sming/issues/378/events","html_url":"https://github.com/SmingHub/Sming/issues/378","id":113678139,"number":378,"title":"Possible memory leak in JsonObject.cpp","user":{"login":"linlun","id":1452982,"avatar_url":"https://avatars.githubusercontent.com/u/1452982?v=3","gravatar_id":"","url":"https://api.github.com/users/linlun","html_url":"https://github.com/linlun","followers_url":"https://api.github.com/users/linlun/followers","following_url":"https://api.github.com/users/linlun/following{/other_user}","gists_url":"https://api.github.com/users/linlun/gists{/gist_id}","starred_url":"https://api.github.com/users/linlun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/linlun/subscriptions","organizations_url":"https://api.github.com/users/linlun/orgs","repos_url":"https://api.github.com/users/linlun/repos","events_url":"https://api.github.com/users/linlun/events{/privacy}","received_events_url":"https://api.github.com/users/linlun/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2015-10-27T20:17:10Z","updated_at":"2015-11-01T10:38:09Z","closed_at":null,"body":"I have a program that is returning an JSON object as a response to a httprequest looking like this:\r\n```cpp\r\nvoid onApiSensors(HttpRequest &request, HttpResponse &response)\r\n{\r\n\tJsonObjectStream* stream = new JsonObjectStream();\r\n\tJsonObject& json = stream->getRoot();\r\n\tjson[\"status\"] = (bool)true;\r\n\tjson[\"heap\"] = system_get_free_heap_size();\r\n\tsensors[\"temperature\"] = tempSensor.get();\r\n\tfor( int i = 0; i < tempSensor.numSensors; i++)\r\n\t{\r\n\t\tchar buff[3];\r\n\t\titoa(i, buff, 10);\r\n\t\tString desiredString = \"sensor_\";\r\n\t\tdesiredString += buff;\r\n\t\tsensors[desiredString] = tempSensor.get(i);\r\n\t}\r\n\tJsonObject& pid = json.createNestedObject(\"pid\");\r\n\tpid[\"Setpoint\"] = AppSettings.Setpoint;\r\n\tresponse.sendJsonObject(stream);\r\n}\r\n```\r\nBut on each request the heap size get smaller, the issue has been tracked down to the line `sensors[desiredString] = tempSensor.get(i);`. \r\nIt seems as the Json implementation does not free dynamic memory as it should, the problem is most likely in https://github.com/SmingHub/Sming/blob/master/Sming/Services/ArduinoJson/src/JsonObject.cpp on row 44 where the memory is not freed.\r\n\r\nA new version looking like this does not have an issue with memory leakage:\r\n```cpp\r\nconst char* names[] = { \"sensor_01\", \"sensor_02\", \"sensor_03\", \"sensor_04\", \"sensor_05\", \"sensor_06\" };\r\nvoid onApiSensors(HttpRequest &request, HttpResponse &response)\r\n{\r\n\tJsonObjectStream* stream = new JsonObjectStream();\r\n\tJsonObject& json = stream->getRoot();\r\n\tjson[\"status\"] = (bool)true;\r\n\tjson[\"heap\"] = system_get_free_heap_size();\r\n\tJsonObject& sensors = json.createNestedObject(\"sensors\");\r\n\tsensors[\"temperature\"] = tempSensor.get();\r\n\tfor( int i = 0; i < tempSensor.numSensors; i++)\r\n\t{\r\n\t\tsensors[names[i]] = tempSensor.get(i);\r\n\t}\r\n\tJsonObject& pid = json.createNestedObject(\"pid\");\r\n\tpid[\"Setpoint\"] = AppSettings.Setpoint;\r\n\tresponse.sendJsonObject(stream);\r\n}\r\n``` \r\n\r\nRegards Linus"},"comment":{"url":"https://api.github.com/repos/SmingHub/Sming/issues/comments/152815410","html_url":"https://github.com/SmingHub/Sming/issues/378#issuecomment-152815410","issue_url":"https://api.github.com/repos/SmingHub/Sming/issues/378","id":152815410,"user":{"login":"hreintke","id":4587225,"avatar_url":"https://avatars.githubusercontent.com/u/4587225?v=3","gravatar_id":"","url":"https://api.github.com/users/hreintke","html_url":"https://github.com/hreintke","followers_url":"https://api.github.com/users/hreintke/followers","following_url":"https://api.github.com/users/hreintke/following{/other_user}","gists_url":"https://api.github.com/users/hreintke/gists{/gist_id}","starred_url":"https://api.github.com/users/hreintke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hreintke/subscriptions","organizations_url":"https://api.github.com/users/hreintke/orgs","repos_url":"https://api.github.com/users/hreintke/repos","events_url":"https://api.github.com/users/hreintke/events{/privacy}","received_events_url":"https://api.github.com/users/hreintke/received_events","type":"User","site_admin":false},"created_at":"2015-11-01T10:38:09Z","updated_at":"2015-11-01T10:38:09Z","body":"@vsirvent : \r\nPlease check documentation of ArduioJson on : \r\n- http://blog.benoitblanchon.fr/arduino-json-v5-0/\r\n- https://github.com/bblanchon/ArduinoJson\r\n\r\nI think what you describe is expected behavior of the package.\r\nMy goal is to use the package without local updates.\r\n\r\n"}}