@@ -122,14 +122,11 @@ static char** save_argv;
122
122
123
123
124
124
/*
125
- * Call this early in startup to save the original argc/argv values.
126
- * If needed, we make a copy of the original argv[] array to preserve it
127
- * from being clobbered by subsequent ps_display actions.
128
- *
129
- * (The original argv[] will not be overwritten by this routine.
130
- * Also, the physical location of the environment strings may be moved,
131
- * so this should be called before any code that might try to hang onto a
132
- * getenv() result.)
125
+ * Call this method early, before any code has used the original argv passed in
126
+ * from main().
127
+ * If needed, this code will make deep copies of argv and environ and return
128
+ * these to the caller for further use. The original argv is then 'clobbered'
129
+ * to store the process title.
133
130
*/
134
131
char * * save_ps_args (int argc , char * * argv )
135
132
{
@@ -388,3 +385,41 @@ int get_ps_title(int *displen, const char** string)
388
385
return PS_TITLE_SUCCESS ;
389
386
}
390
387
388
+ /*
389
+ * Clean up the allocated argv and environ if applicable. Only call
390
+ * this right before exiting.
391
+ * This isn't needed per-se because the OS will clean-up anyway, but
392
+ * having and calling this will ensure Valgrind doesn't output 'false
393
+ * positives'.
394
+ */
395
+ void cleanup_ps_args (char * * argv )
396
+ {
397
+ #ifndef PS_USE_NONE
398
+ if (save_argv )
399
+ {
400
+ save_argv = NULL ;
401
+ save_argc = 0 ;
402
+
403
+ #ifdef PS_USE_CLOBBER_ARGV
404
+ {
405
+ // clean up environ
406
+ int i ;
407
+ for (i = 0 ; environ [i ] != NULL ; i ++ )
408
+ free (environ [i ]);
409
+ free (environ );
410
+ }
411
+ #endif /* PS_USE_CLOBBER_ARGV */
412
+
413
+ #if defined(PS_USE_CHANGE_ARGV ) || defined(PS_USE_CLOBBER_ARGV )
414
+ {
415
+ int i ;
416
+ for (i = 0 ; argv [i ] != NULL ; i ++ )
417
+ free (argv [i ]);
418
+ free (argv );
419
+ }
420
+ #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
421
+ }
422
+ #endif /* PS_USE_NONE */
423
+
424
+ return ;
425
+ }
0 commit comments