From d7f7f61c761db10bc63c94e9d93ca60ff8235768 Mon Sep 17 00:00:00 2001
From: "A. Unique TensorFlower" <gardener@tensorflow.org>
Date: Wed, 16 Dec 2020 16:04:54 -0800
Subject: [PATCH] [TF:TRT] Fix memory leakage in
 TRTEngineOp::ExecuteNativeSegment.

The statement `delete outputs` may not be executed in the original implementation.

PiperOrigin-RevId: 347915990
Change-Id: Ic5cee6b19eb55ce5da5b3669ede0059d1889288c
---
 tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc b/tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc
index a1d5cfa7685..c00b4d749c0 100644
--- a/tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc
+++ b/tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc
@@ -461,12 +461,12 @@ void TRTEngineOp::ExecuteNativeSegment(OpKernelContext* ctx,
   lib->Run(opts, native_execution_func_handle_, inputs, outputs,
            [this, ctx, outputs, helper](const Status& s) {
              core::ScopedUnref sc(helper);
+             std::unique_ptr<std::vector<Tensor>> outputs_wrapper(outputs);
              OP_REQUIRES_OK_ASYNC(ctx, s, *helper);
              VLOG(1) << "Native Segment completed";
              for (size_t t = 0; t < outputs->size(); ++t) {
                ctx->set_output(t, outputs->at(t));
              }
-             delete outputs;
            });
 }