Skip to content

Commit

Permalink
Fix memory leak in region extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Oct 26, 2024
1 parent 0509fbd commit 48d3243
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions c_src/vips_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ ERL_NIF_TERM nif_image_write_area_to_binary(ErlNifEnv *env, int argc,
guint list_length;
int params[6] = {0, 0, 0, 0, 0, 0};
int left, top, width, height, band_start, band_count;
VipsImage **t;
VipsImage **t = NULL;

start = enif_monotonic_time(ERL_NIF_USEC);

Expand Down Expand Up @@ -964,14 +964,20 @@ ERL_NIF_TERM nif_image_write_area_to_binary(ErlNifEnv *env, int argc,
goto exit;
}

t = (VipsImage **)vips_object_local_array((VipsObject *)image, 2);
t = VIPS_ARRAY(NULL, 2, VipsImage *);

if (vips_crop(image, &t[0], left, top, width, height, NULL) ||
vips_extract_band(t[0], &t[1], band_start, "n", band_count, NULL)) {
error("Failed to extract region and bands. error: %s", vips_error_buffer());
if (vips_crop(image, &t[0], left, top, width, height, NULL)) {
error("Failed to extract region. error: %s", vips_error_buffer());
vips_error_clear();
ret = make_error(env, "Failed to extract region and bands");
goto exit;
ret = make_error(env, "Failed to extract region");
goto exit_free_temp_arr;
}

if (vips_extract_band(t[0], &t[1], band_start, "n", band_count, NULL)) {
error("Failed to extract bands. error: %s", vips_error_buffer());
vips_error_clear();
ret = make_error(env, "Failed to extract bands");
goto exit_free_temp_first;
}

bin = vips_image_write_to_memory(t[1], &size);
Expand All @@ -981,7 +987,7 @@ ERL_NIF_TERM nif_image_write_area_to_binary(ErlNifEnv *env, int argc,
vips_error_buffer());
vips_error_clear();
ret = make_error(env, "Failed to write extracted region to memory");
goto exit;
goto exit_free_temp_both;
}

ret = make_ok(
Expand All @@ -991,6 +997,15 @@ ERL_NIF_TERM nif_image_write_area_to_binary(ErlNifEnv *env, int argc,
enif_make_int(env, vips_image_get_bands(t[1])),
enif_make_int(env, vips_image_get_format(t[1]))));

exit_free_temp_both:
g_object_unref(t[1]);

exit_free_temp_first:
g_object_unref(t[0]);

exit_free_temp_arr:
g_free(t);

exit:
notify_consumed_timeslice(env, start, enif_monotonic_time(ERL_NIF_USEC));
return ret;
Expand Down

0 comments on commit 48d3243

Please sign in to comment.