Fix memory leak in srcn (#7162)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 4 Sep 2019 08:18:45 +0000 (17:18 +0900)
committer이춘석/On-Device Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Wed, 4 Sep 2019 08:18:45 +0000 (17:18 +0900)
Delete allocated array before fast return

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/libs/srcn/src/conv_winograd.cc
runtimes/libs/srcn/src/conv_winograd_batch.cc
runtimes/libs/srcn/src/srcn_conv.cc

index 0e96d17..cc11498 100644 (file)
@@ -277,25 +277,30 @@ void conv_winograd::compute_winograd()
   /*Step 2: transfer image to winograd domain*/
   float *col_buff =
       new float[std::max(outch, inch) * ntiles_h_ * ntiles_w_ * tile_h_in_ * tile_w_in_];
-  if (NULL == col_buff)
-    return;
-
-  winograd_input_im2col(col_buff);
 
   int temp1_n = inch * ntiles_h_ * ntiles_w_;
   float *temp1_ =
       new float[tile_h_in_ * tile_w_in_ * std::max(outch, inch) * ntiles_h_ * ntiles_w_];
-  if (NULL == temp1_)
-    return;
 
   float *winograd_b = new float[M * M * M * M];
-  if (NULL == winograd_b)
+
+  if ((NULL == col_buff) || (NULL == temp1_) || (NULL == winograd_b))
+  {
+    delete[] col_buff;
+    delete[] temp1_;
+    delete[] winograd_b;
     return;
+  }
+
+  winograd_input_im2col(col_buff);
+
   kronecker_product(winograd_b, B, B, M, M, M, M);
 
   compute_sgemm(rowMajor, trans, trans, tile_h_in_ * tile_w_in_, temp1_n, tile_h_in_ * tile_w_in_,
                 winograd_b, col_buff, temp1_);
 
+  delete[] winograd_b;
+
   /*Step 3: convolution in winograd domain*/
   for (int j = 0; j < tile_h_in_ * tile_w_in_; ++j)
   {
@@ -308,7 +313,11 @@ void conv_winograd::compute_winograd()
   /*Step 4: transfer back to time domain*/
   float *winograd_a = new float[M * (M - N + 1) * M * (M - N + 1)];
   if (NULL == winograd_a)
+  {
+    delete[] col_buff;
+    delete[] temp1_;
     return;
+  }
   kronecker_product(winograd_a, A, A, M, M - N + 1, M, M - N + 1);
   compute_sgemm(rowMajor, trans, notrans, outch * ntiles_h_ * ntiles_w_, tile_h_out_ * tile_w_out_,
                 tile_h_in_ * tile_w_in_, col_buff, winograd_a, temp1_);
@@ -318,8 +327,6 @@ void conv_winograd::compute_winograd()
   winograd_output_col2im(temp1_);
 
   delete[] temp1_;
-
-  delete[] winograd_b;
 }
 
 void conv_winograd::run()
index 97e35f3..7b468db 100644 (file)
@@ -240,24 +240,28 @@ void conv_winograd_batch::compute_winograd()
   /*Step 2: transfer image to winograd domain*/
   float *col_buff =
       new float[std::max(outch, inch) * batch * ntiles_h_ * ntiles_w_ * tile_h_in_ * tile_w_in_];
-  if (NULL == col_buff)
-    return;
-
-  winograd_input_im2col(col_buff);
 
   int temp1_n = batch * inch * ntiles_h_ * ntiles_w_;
   float *temp1_ =
       new float[batch * tile_h_in_ * tile_w_in_ * std::max(outch, inch) * ntiles_h_ * ntiles_w_];
-  if (NULL == temp1_)
-    return;
 
   float *winograd_b = new float[M * M * M * M];
-  if (NULL == winograd_b)
+
+  if ((NULL == col_buff) || (NULL == temp1_) || (NULL == winograd_b))
+  {
+    delete[] col_buff;
+    delete[] temp1_;
+    delete[] winograd_b;
     return;
+  }
+
+  winograd_input_im2col(col_buff);
+
   kronecker_product(winograd_b, B, B, M, M, M, M);
 
   compute_sgemm(rowMajor, trans, trans, tile_h_in_ * tile_w_in_, temp1_n, tile_h_in_ * tile_w_in_,
                 winograd_b, col_buff, temp1_);
+  delete[] winograd_b;
 
   /*Step 3: convolution in winograd domain*/
   for (int j = 0; j < tile_h_in_ * tile_w_in_; ++j)
@@ -271,7 +275,12 @@ void conv_winograd_batch::compute_winograd()
   /*Step 4: transfer back to time domain*/
   float *winograd_a = new float[M * (M - N + 1) * M * (M - N + 1)];
   if (NULL == winograd_a)
+  {
+    delete[] col_buff;
+    delete[] temp1_;
     return;
+  }
+
   kronecker_product(winograd_a, A, A, M, M - N + 1, M, M - N + 1);
   compute_sgemm(rowMajor, trans, notrans, batch * outch * ntiles_h_ * ntiles_w_,
                 tile_h_out_ * tile_w_out_, tile_h_in_ * tile_w_in_, col_buff, winograd_a, temp1_);
@@ -281,8 +290,6 @@ void conv_winograd_batch::compute_winograd()
   winograd_output_col2im(temp1_);
 
   delete[] temp1_;
-
-  delete[] winograd_b;
 }
 
 void conv_winograd_batch::run()
index 16ee268..df2c871 100644 (file)
@@ -161,7 +161,10 @@ float *trans_weight2winograd(winogradParams_t &params, unsigned int *size = NULL
 
   float *winograd_g = new float[M * M * N * N];
   if (!winograd_g)
+  {
+    delete[] winograd_weight;
     return NULL;
+  }
 
   kronecker_product(winograd_g, G, G, M, N, M, N);
 
@@ -169,7 +172,11 @@ float *trans_weight2winograd(winogradParams_t &params, unsigned int *size = NULL
   {
     weight_data = new float[kernel_size * kernel_size * inch * outch];
     if (!weight_data)
+    {
+      delete[] winograd_weight;
+      delete[] winograd_g;
       return NULL;
+    }
     weight_transfer(weight_data, params.weight_data, kernel_size, kernel_size, inch, outch);
   }