This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 3b6f6ab4e6e4279ed596ffc3e0a0997591a854a4
Author: chenrun1 <[email protected]>
AuthorDate: Wed Apr 3 16:44:57 2024 +0800

    ramspeed:Fix the mem leak caused by not releasing memory when the task 
exits under the -a option
    
    Signed-off-by: chenrun1 <[email protected]>
---
 benchmarks/ramspeed/ramspeed_main.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/benchmarks/ramspeed/ramspeed_main.c 
b/benchmarks/ramspeed/ramspeed_main.c
index 24706e11b..9343de9a4 100644
--- a/benchmarks/ramspeed/ramspeed_main.c
+++ b/benchmarks/ramspeed/ramspeed_main.c
@@ -87,6 +87,7 @@ struct ramspeed_s
   uint8_t value;
   uint32_t repeat_num;
   bool irq_disable;
+  bool allocate_rw_address;
 };
 
 /****************************************************************************
@@ -130,7 +131,6 @@ static void parse_commandline(int argc, FAR char **argv,
                               FAR struct ramspeed_s *info)
 {
   int ch;
-  bool allocate_rw_address = false;
 
   memset(info, 0, sizeof(struct ramspeed_s));
   info->repeat_num = 100;
@@ -146,7 +146,7 @@ static void parse_commandline(int argc, FAR char **argv,
       switch (ch)
         {
           case 'a':
-            allocate_rw_address = true;
+            info->allocate_rw_address = true;
             break;
           case 'r':
             OPTARG_TO_VALUE(info->src, const void *, 16);
@@ -187,10 +187,20 @@ static void parse_commandline(int argc, FAR char **argv,
         }
     }
 
-  if (allocate_rw_address)
+  if (info->allocate_rw_address)
     {
       info->dest = malloc(info->size);
+      if (info->dest == NULL)
+        {
+          printf(RAMSPEED_PREFIX "Dest Alloc Memory Failed!\n");
+        }
+
       info->src = malloc(info->size);
+      if (info->src == NULL)
+        {
+          free(info->dest);
+          printf(RAMSPEED_PREFIX "Src Alloc Memory Failed!\n");
+        }
     }
 
   if (info->dest == NULL || info->src == NULL || info->size == 0)
@@ -533,5 +543,13 @@ int main(int argc, FAR char *argv[])
                     ramspeed.size, ramspeed.repeat_num,
                     ramspeed.irq_disable);
 
+  /* Check if alloc from heap? */
+
+  if (ramspeed.allocate_rw_address)
+    {
+      free(ramspeed.dest);
+      free((void *)ramspeed.src);
+    }
+
   return EXIT_SUCCESS;
 }

Reply via email to