From 63a567ad8a7f138e85062628928a5b0684755c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arttu=20Yl=C3=A4-Outinen?= Date: Sun, 5 Feb 2017 19:43:12 +0900 Subject: [PATCH] Fix leaking memory when --roi given many times Any previously allocated delta QP array was not freed when allocating a new array. --- src/cfg.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 18fa7d58..d8e8ef06 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -1023,17 +1023,20 @@ int kvz_config_parse(kvz_config *cfg, const char *name, const char *value) fprintf(stderr, "ROI dimensions exceed arbitrary value of 10000.\n"); return 0; } - const unsigned size = width * height; - cfg->roi.width = width; - cfg->roi.height = height; - cfg->roi.dqps = calloc((size_t)size, sizeof(*cfg->roi.dqps)); - if (!cfg->roi.dqps) { + const unsigned size = width * height; + uint8_t *dqp_array = calloc((size_t)size, sizeof(cfg->roi.dqps[0])); + if (!dqp_array) { fprintf(stderr, "Failed to allocate memory for ROI table.\n"); fclose(f); return 0; } + FREE_POINTER(cfg->roi.dqps); + cfg->roi.dqps = dqp_array; + cfg->roi.width = width; + cfg->roi.height = height; + for (int i = 0; i < size; ++i) { int number; // Need a pointer to int for fscanf if (fscanf(f, "%d", &number) != 1) { @@ -1041,7 +1044,7 @@ int kvz_config_parse(kvz_config *cfg, const char *name, const char *value) fclose(f); return 0; } - cfg->roi.dqps[i] = (uint8_t)number; + dqp_array[i] = (uint8_t)number; } fclose(f);