fix memory leaks

This commit is contained in:
Max Bruckner 2015-10-14 02:43:18 +02:00
parent 1a20eb8494
commit 58bc383b28
2 changed files with 15 additions and 4 deletions

View File

@ -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;
}

View File

@ -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();