浏览代码

[bug fix]: Valgrind report memory leak in main_ctx_init (#181)

* [bug fix]: Valgrind report memory leak. When main_ctx_init function is used, is it because are made 5 SAFE_LLOC in lines 119/120/122/147 and 148. This patch add the main_ctx_finish function to allow to call and free resources in the end of main function.
SLT 3 年之前
父节点
当前提交
6601cc8d76
共有 2 个文件被更改,包括 10 次插入0 次删除
  1. 8 0
      base/hmain.c
  2. 2 0
      base/hmain.h

+ 8 - 0
base/hmain.c

@@ -168,6 +168,14 @@ int main_ctx_init(int argc, char** argv) {
     return 0;
     return 0;
 }
 }
 
 
+void main_ctx_finish() {
+    SAFE_FREE( g_main_ctx.save_argv[ 0 ] );
+    SAFE_FREE( g_main_ctx.save_argv );
+    SAFE_FREE( g_main_ctx.cmdline );
+    SAFE_FREE( g_main_ctx.save_envp[ 0 ] );
+    SAFE_FREE( g_main_ctx.save_envp );
+}
+
 #define UNDEFINED_OPTION    -1
 #define UNDEFINED_OPTION    -1
 static int get_arg_type(int short_opt, const char* options) {
 static int get_arg_type(int short_opt, const char* options) {
     if (options == NULL) return UNDEFINED_OPTION;
     if (options == NULL) return UNDEFINED_OPTION;

+ 2 - 0
base/hmain.h

@@ -68,6 +68,8 @@ typedef struct option_s {
 } option_t;
 } option_t;
 
 
 HV_EXPORT int main_ctx_init(int argc, char** argv);
 HV_EXPORT int main_ctx_init(int argc, char** argv);
+HV_EXPORT void main_ctx_finish();
+
 // ls -a -l
 // ls -a -l
 // ls -al
 // ls -al
 // watch -n 10 ls
 // watch -n 10 ls