Fix more memory leaks in json.

By doing a little reference tracking, we can make sure we don't miss any
JsonValues that may be floating around by the time we're done.
This commit is contained in:
Jordan Bancino 2023-05-26 22:11:07 +00:00
parent efb27c9db8
commit d81600d944
2 changed files with 14 additions and 2 deletions

View file

@ -11,7 +11,8 @@ Key:
Milestone: v0.3.0 Milestone: v0.3.0
----------------- -----------------
[ ] Fix leaks in json [x] Fix leaks in json
[ ] Debug ARM64 Debian
[ ] Cytoplasm [ ] Cytoplasm
[ ] Debug OpenSSL [ ] Debug OpenSSL

View file

@ -47,6 +47,10 @@ query(char *select, HashMap * json)
char *key; char *key;
JsonValue *rootVal = JsonValueObject(json); JsonValue *rootVal = JsonValueObject(json);
JsonValue *val = rootVal; JsonValue *val = rootVal;
Array *cleanUp = ArrayCreate();
size_t i;
ArrayAdd(cleanUp, rootVal);
key = strtok(select, "->"); key = strtok(select, "->");
@ -67,9 +71,11 @@ query(char *select, HashMap * json)
{ {
case JSON_ARRAY: case JSON_ARRAY:
val = JsonValueInteger(ArraySize(JsonValueAsArray(val))); val = JsonValueInteger(ArraySize(JsonValueAsArray(val)));
ArrayAdd(cleanUp, val);
break; break;
case JSON_STRING: case JSON_STRING:
val = JsonValueInteger(strlen(JsonValueAsString(val))); val = JsonValueInteger(strlen(JsonValueAsString(val)));
ArrayAdd(cleanUp, val);
break; break;
default: default:
val = NULL; val = NULL;
@ -89,6 +95,7 @@ query(char *select, HashMap * json)
} }
val = JsonValueArray(arr); val = JsonValueArray(arr);
ArrayAdd(cleanUp, val);
} }
else if (JsonValueType(val) == JSON_STRING && StrEquals(keyName + 1, "decode")) else if (JsonValueType(val) == JSON_STRING && StrEquals(keyName + 1, "decode"))
{ {
@ -167,7 +174,11 @@ query(char *select, HashMap * json)
StreamPutc(StreamStdout(), '\n'); StreamPutc(StreamStdout(), '\n');
} }
JsonValueFree(rootVal); for (i = 0; i < ArraySize(cleanUp); i++)
{
JsonValueFree(ArrayGet(cleanUp, i));
}
ArrayFree(cleanUp);
} }
static void static void