Release reply Parcel when transaction is done.

To prevent the binder memory window from overflowing.

Test: hidl_test_java
Bug: 36088202
Change-Id: I1261628f31c0aa3ecba7e93630a2eb33229d377b
(cherry picked from commit 4de083b1791414410fd9add633ccbb8e56b15036)
diff --git a/generateJava.cpp b/generateJava.cpp
index 453bb17..0694a82 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -380,60 +380,65 @@
                     false /* addPrefixToName */);
         }
 
-        out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n"
-            << "mRemote.transact("
-            << method->getSerialId()
-            << " /* "
-            << method->name()
-            << " */, _hidl_request, _hidl_reply, ";
+        out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
 
-        if (method->isOneway()) {
-            out << "android.os.IHwBinder.FLAG_ONEWAY";
-        } else {
-            out << "0 /* flags */";
-        }
+        out.sTry([&] {
+            out << "mRemote.transact("
+                << method->getSerialId()
+                << " /* "
+                << method->name()
+                << " */, _hidl_request, _hidl_reply, ";
 
-        out << ");\n";
-
-        if (!method->isOneway()) {
-            out << "_hidl_reply.verifySuccess();\n";
-        } else {
-            CHECK(!returnsValue);
-        }
-
-        out << "_hidl_request.releaseTemporaryStorage();\n";
-
-        if (returnsValue) {
-            out << "\n";
-
-            for (const auto &arg : method->results()) {
-                emitJavaReaderWriter(
-                        out,
-                        "_hidl_reply",
-                        arg,
-                        true /* isReader */,
-                        true /* addPrefixToName */);
+            if (method->isOneway()) {
+                out << "android.os.IHwBinder.FLAG_ONEWAY";
+            } else {
+                out << "0 /* flags */";
             }
 
-            if (needsCallback) {
-                out << "cb.onValues(";
+            out << ");\n";
 
-                bool firstField = true;
+            if (!method->isOneway()) {
+                out << "_hidl_reply.verifySuccess();\n";
+            } else {
+                CHECK(!returnsValue);
+            }
+
+            out << "_hidl_request.releaseTemporaryStorage();\n";
+
+            if (returnsValue) {
+                out << "\n";
+
                 for (const auto &arg : method->results()) {
-                    if (!firstField) {
-                        out << ", ";
-                    }
-
-                    out << "_hidl_out_" << arg->name();
-                    firstField = false;
+                    emitJavaReaderWriter(
+                            out,
+                            "_hidl_reply",
+                            arg,
+                            true /* isReader */,
+                            true /* addPrefixToName */);
                 }
 
-                out << ");\n";
-            } else {
-                const std::string returnName = method->results()[0]->name();
-                out << "return _hidl_out_" << returnName << ";\n";
+                if (needsCallback) {
+                    out << "cb.onValues(";
+
+                    bool firstField = true;
+                    for (const auto &arg : method->results()) {
+                        if (!firstField) {
+                            out << ", ";
+                        }
+
+                        out << "_hidl_out_" << arg->name();
+                        firstField = false;
+                    }
+
+                    out << ");\n";
+                } else {
+                    const std::string returnName = method->results()[0]->name();
+                    out << "return _hidl_out_" << returnName << ";\n";
+                }
             }
-        }
+        }).sFinally([&] {
+            out << "_hidl_reply.release();\n";
+        }).endl();
 
         out.unindent();
         out << "}\n\n";
diff --git a/utils/Formatter.cpp b/utils/Formatter.cpp
index 4d3771b..db763d9 100644
--- a/utils/Formatter.cpp
+++ b/utils/Formatter.cpp
@@ -96,6 +96,11 @@
     return this->block(block);
 }
 
+Formatter &Formatter::sFinally(std::function<void(void)> block) {
+    (*this) << " finally ";
+    return this->block(block);
+}
+
 Formatter &Formatter::operator<<(const std::string &out) {
     const size_t len = out.length();
     size_t start = 0;
diff --git a/utils/include/hidl-util/Formatter.h b/utils/include/hidl-util/Formatter.h
index d1f76fb..7e0c4f9 100644
--- a/utils/include/hidl-util/Formatter.h
+++ b/utils/include/hidl-util/Formatter.h
@@ -83,10 +83,13 @@
     //     out << "throw RemoteException();\n"
     // }).sCatch("RemoteException ex", [&] {
     //     out << "ex.printStackTrace();\n"
+    // }).sFinally([&] {
+    //     // cleanup
     // }).endl();
     // note that there will be a space before the "catch"-s.
     Formatter &sTry(std::function<void(void)> block);
     Formatter &sCatch(const std::string &exception, std::function<void(void)> block);
+    Formatter &sFinally(std::function<void(void)> block);
 
     Formatter &operator<<(const std::string &out);
     Formatter &operator<<(size_t n);