cJSON

mirror of Dave's cJSON
git clone git://git.thc420.xyz/cJSON
Log | Files | Refs | README | LICENSE

commit 99c46e7661a8d7d8196511bddfe587cb63106d8f
parent c7241f5314b52ad748c24d8f76087e0be4835d09
Author: Kevin Branigan <kbranigan@gmail.com>
Date:   Sun, 18 Oct 2015 17:03:25 -0400

Merge pull request #20 from FSMaxB/fix_memory_leaks

fix memory leaks

Diffstat:
McJSON_Utils.c | 6+++++-
Mtest_utils.c | 13++++++++++---
2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/cJSON_Utils.c b/cJSON_Utils.c @@ -186,7 +186,7 @@ static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) cJSONUtils_InplaceDecodePointerString(childptr); /* add, remove, replace, move, copy, test. */ - if (!parent) {free(parentptr); return 9;} /* Couldn't find object to add to. */ + if (!parent) {free(parentptr); cJSON_Delete(value); return 9;} /* Couldn't find object to add to. */ else if (parent->type==cJSON_Array) { if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value); @@ -197,6 +197,10 @@ static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) cJSON_DeleteItemFromObject(parent,childptr); cJSON_AddItemToObject(parent,childptr,value); } + else + { + cJSON_Delete(value); + } free(parentptr); return 0; } diff --git a/test_utils.c b/test_utils.c @@ -91,9 +91,16 @@ int main() nums=cJSON_CreateIntArray(numbers,10); num6=cJSON_GetArrayItem(nums,6); cJSON_AddItemToObject(object,"numbers",nums); - printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,num6)); - printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,nums)); - printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,object)); + char *temp=cJSONUtils_FindPointerFromObjectTo(object,num6); + printf("Pointer: [%s]\n",temp); + free(temp); + temp=cJSONUtils_FindPointerFromObjectTo(object,nums); + printf("Pointer: [%s]\n",temp); + free(temp); + temp=cJSONUtils_FindPointerFromObjectTo(object,object); + printf("Pointer: [%s]\n",temp); + free(temp); + cJSON_Delete(object); /* JSON Sort test: */ sortme=cJSON_CreateObject();