Skip to content

Commit

Permalink
automatically free plugin (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 7, 2024
1 parent c2837d2 commit a720cde
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions libheif/codecs/decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ Decoder::decode_single_frame_from_compressed_data(const struct heif_decoding_opt
return Error(err.code, err.subcode, err.message);
}

// automatically delete decoder plugin when we leave the scope
auto pluginDestructor = [decoder_plugin](void* decoder) { decoder_plugin->free_decoder(decoder); };
std::unique_ptr<void, decltype(pluginDestructor)> decoderSmartPtr(decoder, pluginDestructor);

if (decoder_plugin->plugin_api_version >= 2) {
if (decoder_plugin->set_strict_decoding) {
decoder_plugin->set_strict_decoding(decoder, options.strict_decoding);
Expand All @@ -189,27 +193,23 @@ Decoder::decode_single_frame_from_compressed_data(const struct heif_decoding_opt

auto dataResult = get_compressed_data();
if (dataResult.error) {
decoder_plugin->free_decoder(decoder);
return dataResult.error;
}

err = decoder_plugin->push_data(decoder, dataResult.value.data(), dataResult.value.size());
if (err.code != heif_error_Ok) {
decoder_plugin->free_decoder(decoder);
return Error(err.code, err.subcode, err.message);
}

heif_image* decoded_img = nullptr;

err = decoder_plugin->decode_image(decoder, &decoded_img);
if (err.code != heif_error_Ok) {
decoder_plugin->free_decoder(decoder);
return Error(err.code, err.subcode, err.message);
}

if (!decoded_img) {
// TODO(farindk): The plugin should return an error in this case.
decoder_plugin->free_decoder(decoder);
return Error(heif_error_Decoder_plugin_error, heif_suberror_Unspecified);
}

Expand All @@ -218,7 +218,5 @@ Decoder::decode_single_frame_from_compressed_data(const struct heif_decoding_opt
std::shared_ptr<HeifPixelImage> img = std::move(decoded_img->image);
heif_image_release(decoded_img);

decoder_plugin->free_decoder(decoder);

return img;
}

0 comments on commit a720cde

Please sign in to comment.