You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/03/30 19:16:31 UTC

[incubator-nuttx-apps] branch master updated: testing/mm: repair the memory leak question when realloc failed.

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/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 963c1f3  testing/mm: repair the memory leak question when realloc failed.
963c1f3 is described below

commit 963c1f31db449b059988b04c660e520303f28bcb
Author: wangbowen6 <wa...@xiaomi.com>
AuthorDate: Wed Mar 30 22:26:28 2022 +0800

    testing/mm: repair the memory leak question when realloc failed.
    
    Signed-off-by: wangbowen6 <wa...@xiaomi.com>
---
 testing/mm/mm_main.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/testing/mm/mm_main.c b/testing/mm/mm_main.c
index 4517b6d..d4c28a4 100644
--- a/testing/mm/mm_main.c
+++ b/testing/mm/mm_main.c
@@ -177,6 +177,7 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
 {
   int i;
   int j;
+  void *ptr;
 
   for (i = 0; i < n; i++)
     {
@@ -184,10 +185,19 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
       printf("(%d)Re-allocating at %p from %d to %d bytes\n",
              i, mem[j], oldsize[j], newsize[j]);
 
-      mem[j] = realloc(mem[j], newsize[j]);
-      printf("(%d)Memory re-allocated at %p\n", i, mem[j]);
+      /* Return null if realloc failed, so using a local variable to store
+       * the return value to avoid the missing of old memory pointer.
+       */
 
-      if (mem[j] == NULL)
+      ptr = realloc(mem[j], newsize[j]);
+      if (ptr != NULL)
+        {
+          mem[j] = ptr;
+        }
+
+      printf("(%d)Memory re-allocated at %p\n", i, ptr);
+
+      if (ptr == NULL)
         {
           int allocsize = MM_ALIGN_UP(newsize[j] + SIZEOF_MM_ALLOCNODE);