Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heif_encoder_aom.cc: Fix input_image leak on error #801

Merged

Conversation

wantehchang
Copy link
Contributor

No description provided.

@farindk
Copy link
Contributor

farindk commented Mar 30, 2023

Thank you. I've added a comment to make it clear why the image is (very creatively) released at that very position.

@wantehchang wantehchang deleted the fix-input-image-leak-on-encode-error branch March 30, 2023 18:52
@wantehchang
Copy link
Contributor Author

Thank you, Dirk.

Another solution is to call aom_img_free() inside encode_frame() before it returns an error.

@farindk
Copy link
Contributor

farindk commented Mar 30, 2023

@wantehchang Yes, but then 'alloc' and 'free' would not be on the same (function call) level. I prefer your solution.

@wantehchang
Copy link
Contributor Author

Dirk: In that case, we may want to move the aom_codec_destroy() call in encode_frame() to its caller:

diff --git a/libheif/plugins/heif_encoder_aom.cc b/libheif/plugins/heif_encoder_aom.cc
index 5baa8db..7f80560 100644
--- a/libheif/plugins/heif_encoder_aom.cc
+++ b/libheif/plugins/heif_encoder_aom.cc
@@ -741,7 +741,6 @@ static heif_error encode_frame(encoder_struct_aom* encoder, aom_codec_ctx_t* cod
         heif_suberror_Encoder_encoding,
         encoder->set_aom_error(aom_codec_error_detail(codec))
     };
-    aom_codec_destroy(codec);
     return err;
   }
 
@@ -1029,6 +1028,7 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
   aom_img_free(&input_image);
 
   if (err.code != heif_error_Ok) {
+    aom_codec_destroy(codec);
     return err;
   }
 

farindk added a commit that referenced this pull request Mar 30, 2023
@farindk
Copy link
Contributor

farindk commented Mar 30, 2023

Or even better: remove the useless encode_frame() completely, as in my patch above.

@wantehchang
Copy link
Contributor Author

Dirk: Your patch above brought back the leak of input_image!

Here is one way to fix the leak:

diff --git a/libheif/plugins/heif_encoder_aom.cc b/libheif/plugins/heif_encoder_aom.cc
index 6cac115..692d11d 100644
--- a/libheif/plugins/heif_encoder_aom.cc
+++ b/libheif/plugins/heif_encoder_aom.cc
@@ -1004,6 +1004,11 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
                          0, // only encoding a single frame
                          1,
                          0); // no flags
+
+  // Note: we are freeing the input image directly after use.
+  // This covers the usual success case and also all error cases that occur below.
+  aom_img_free(&input_image);
+
   if (res != AOM_CODEC_OK) {
     err = {
         heif_error_Encoder_plugin_error,
@@ -1014,15 +1019,6 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
     return err;
   }
 
-
-  // Note: we are freeing the input image directly after use.
-  // This covers the usual success case and also all error cases that occur below.
-  aom_img_free(&input_image);
-
-  if (err.code != heif_error_Ok) {
-    return err;
-  }
-
   encoder->compressedData.clear();
   const aom_codec_cx_pkt_t* pkt = NULL;
   aom_codec_iter_t iter = NULL; // for extracting the compressed packets

@farindk
Copy link
Contributor

farindk commented Mar 30, 2023

Thanks. I really should not be working on three different projects at the same time ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants