Skip to content

Commit

Permalink
openjpeg: fix memory leaks
Browse files Browse the repository at this point in the history
These show when running the unit tests under valgrind, or with heif-enc.
  • Loading branch information
bradh committed Jan 17, 2024
1 parent 3e5b81d commit 8895bd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions libheif/plugins/encoder_openjpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_
opj_codec_t* codec = opj_create_compress(codec_format);
success = opj_setup_encoder(codec, &(encoder->parameters), image);
if (!success) {
opj_destroy_codec(codec);
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed to setup OpenJPEG encoder"};
return error;
}
Expand All @@ -397,6 +398,7 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_
size_t bufferSize = 64 * 1024; // 64k
opj_stream_t* stream = opj_stream_create(bufferSize, false /* read only mode */);
if (stream == NULL) {
opj_destroy_codec(codec);
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed to create opj_stream_t"};
return error;
}
Expand All @@ -415,22 +417,31 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_

success = opj_start_compress(codec, image, stream);
if (!success) {
opj_stream_destroy(stream);
opj_destroy_codec(codec);
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_start_compress()"};
return error;
}

success = opj_encode(codec, stream);
if (!success) {
opj_stream_destroy(stream);
opj_destroy_codec(codec);
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_encode()"};
return error;
}

success = opj_end_compress(codec, stream);
if (!success) {
opj_stream_destroy(stream);
opj_destroy_codec(codec);
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_end_compress()"};
return error;
}

opj_stream_destroy(stream);
opj_destroy_codec(codec);

return heif_error_ok;
}

Expand Down Expand Up @@ -531,6 +542,8 @@ struct heif_error opj_encode_image(void* encoder_raw, const struct heif_image* i
//Encodes the image into a 'codestream' which is stored in the 'encoder' variable
err = generate_codestream(opj_image, encoder);

opj_image_destroy(opj_image);

return err;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/encode_jpeg2000.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static heif_encoding_options * set_encoding_options()
static void do_encode(heif_image* input_image, const char* filename, bool lossless)
{
REQUIRE(input_image != nullptr);

heif_init(nullptr);
heif_context *ctx = heif_context_alloc();
heif_encoder *encoder;
struct heif_error err;
Expand All @@ -70,6 +70,7 @@ static void do_encode(heif_image* input_image, const char* filename, bool lossle
heif_image_release(input_image);

heif_context_free(ctx);
heif_deinit();
}

TEST_CASE("Encode JPEG2000 lossy")
Expand Down

0 comments on commit 8895bd9

Please sign in to comment.