[binutils-gdb] gprofng: fix a memory leak in the mxv-pthreads example

Vladimir Mezentsev vmezents@sourceware.org
Fri Oct 18 03:20:58 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d6a07eeabbadbf846da7d6841340fc589d9a57aa

commit d6a07eeabbadbf846da7d6841340fc589d9a57aa
Author: Ruud van der Pas <ruud.vanderpas@oracle.com>
Date:   Wed Oct 16 16:12:06 2024 +0000

    gprofng: fix a memory leak in the mxv-pthreads example
    
    Fix a bug where the main program does not free the rows of
    the matrix. The memory for thread_data_arguments is also
    not released. In function check_results, the memory for the
    marker vector is not released.
    The usage of the verbose veriable has been extended to
    print more messages.
    
    gprofng/ChangeLog
    2024-10-16  Ruud van der Pas  <ruud.vanderpas@oracle.com>
    
            PR 32273
            PR 32274
            * mxv-pthreads/src/main.c: add calls to free() to
            release the memory allocated for array A and vector
            marker. Improve the usage of the verbose variable.
            * mxv-pthreads/src/manage_data.c: add a diagnostic
            printf statement.
            * mxv-pthreads/src/mydefs.h: adapt prototype to
            match the changes in main.c.

Diff:
---
 gprofng/examples/mxv-pthreads/src/main.c        | 20 +++++++++++++-------
 gprofng/examples/mxv-pthreads/src/manage_data.c |  6 ++++--
 gprofng/examples/mxv-pthreads/src/mydefs.h      |  3 +--
 3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/gprofng/examples/mxv-pthreads/src/main.c b/gprofng/examples/mxv-pthreads/src/main.c
index 625c60484d1..8596763b918 100644
--- a/gprofng/examples/mxv-pthreads/src/main.c
+++ b/gprofng/examples/mxv-pthreads/src/main.c
@@ -28,9 +28,10 @@
 
 #include "mydefs.h"
 
+bool verbose;
+
 int main (int argc, char **argv)
 {
-  bool verbose = false;
 
   thread_data *thread_data_arguments;
   pthread_t   *pthread_ids;
@@ -62,8 +63,7 @@ int main (int argc, char **argv)
 			&number_of_rows,
 			&number_of_columns,
 			&repeat_count,
-			&number_of_threads,
-			&verbose);
+			&number_of_threads);
 
   if (verbose) printf ("Verbose mode enabled\n");
 
@@ -191,11 +191,16 @@ int main (int argc, char **argv)
 * Release the allocated memory and end execution.
 * -----------------------------------------------------------------------------
 */
+  for (int64_t i=0; i<number_of_rows; i++)
+    {
+      free (A[i]);
+    }
   free (A);
   free (b);
   free (c);
   free (ref);
   free (pthread_ids);
+  free (thread_data_arguments);
 
   return (0);
 }
@@ -211,8 +216,7 @@ int get_user_options (int argc, char *argv[],
 		      int64_t *number_of_rows,
 		      int64_t *number_of_columns,
 		      int64_t *repeat_count,
-		      int64_t *number_of_threads,
-		      bool    *verbose)
+		      int64_t *number_of_threads)
 {
   int      opt;
   int      errors		     = 0;
@@ -226,7 +230,7 @@ int get_user_options (int argc, char *argv[],
   *number_of_columns = default_columns;
   *number_of_threads = default_number_of_threads;
   *repeat_count      = default_repeat_count;
-  *verbose	     = default_verbose;
+  verbose	     = default_verbose;
 
   while ((opt = getopt (argc, argv, "m:n:r:t:vh")) != -1)
     {
@@ -245,7 +249,7 @@ int get_user_options (int argc, char *argv[],
 	    *number_of_threads = atol (optarg);
 	    break;
 	  case 'v':
-	    *verbose = true;
+	    verbose = true;
 	    break;
 	  case 'h':
 	  default:
@@ -370,5 +374,7 @@ int64_t check_results (int64_t m, int64_t n, double *c, double *ref)
       printf ("  %c c[%ld] = %f ref[%ld] = %f\n",marker[i],i,c[i],i,ref[i]);
   }
 
+  free (marker);
+
   return (errors);
 }
diff --git a/gprofng/examples/mxv-pthreads/src/manage_data.c b/gprofng/examples/mxv-pthreads/src/manage_data.c
index 3f2891cda5d..9db4496b802 100644
--- a/gprofng/examples/mxv-pthreads/src/manage_data.c
+++ b/gprofng/examples/mxv-pthreads/src/manage_data.c
@@ -20,8 +20,6 @@
 
 #include "mydefs.h"
 
-bool verbose;
-
 /*
 * -----------------------------------------------------------------------------
 * This function allocates the data and sets up the data structures to be used
@@ -66,6 +64,10 @@ void allocate_data (int active_threads,
       perror ("vector ref");
       exit (-1);
     }
+  else
+    {
+      if (verbose) printf ("Vector ref allocated\n");
+    }
 
   if ((*A = (double **) malloc (number_of_rows * sizeof (double))) == NULL)
     {
diff --git a/gprofng/examples/mxv-pthreads/src/mydefs.h b/gprofng/examples/mxv-pthreads/src/mydefs.h
index eae0834cafc..1f7e00a00a5 100644
--- a/gprofng/examples/mxv-pthreads/src/mydefs.h
+++ b/gprofng/examples/mxv-pthreads/src/mydefs.h
@@ -63,8 +63,7 @@ int get_user_options (int     argc,
 		      int64_t *number_of_rows,
 		      int64_t *number_of_columns,
 		      int64_t *repeat_count,
-		      int64_t *number_of_threads,
-		      bool    *verbose);
+		      int64_t *number_of_threads);
 
 void init_data (int64_t m,
 		int64_t n,


More information about the Binutils-cvs mailing list