当我的程序退出时,似乎我的双指针没有被释放。我正在使用以下功能创建environ
的副本。
char **envp_cpy(char **envp)
{
char **copy;
int i;
i = 0;
copy = (char **)malloc((ft_array_len(envp) + 1) * sizeof(char *));
copy[ft_array_len(envp)] = NULL;
if (copy)
{
while (envp[i])
{
//valgrind complains about this line (74 in my file)
copy[i] = ft_strdup(envp[i]);
i++;
}
}
return (copy);
}
我使用setenv
为复制的environ
int ft_setenv(const char *name, const char *val, int ovride, char **envp)
{
char *es;
int i;
if (name == NULL || name[0] == '\0' || ft_strchr(name, '=') != NULL ||
val == NULL)
{
return (-1);
}
if (ft_get_env(name, envp) != NULL && ovride == 0)
return (0);
ft_unsetenv(name, envp);
es = ft_memalloc(ft_strlen(name) + ft_strlen(val) + 2);
if (es == NULL)
return (-1);
ft_strcpy(es, name);
ft_strcat(es, "=");
ft_strcat(es, val);
i = (ft_add_value(es, envp) != 0) ? -1 : 0;
free(es);
return (i);
}
将值添加到environ
int ft_add_value(char *str, char **envp)
{
int i;
i = 0;
while (envp[i])
{
i++;
}
if ((envp[i] = ft_strdup(str))) //valgrind complains about this line
{
envp[++i] = 0;
return (1);
}
return (0);
}
我使用此功能释放副本
void freecopy(char **copy)
{
int i;
i = 0;
while (copy[i])
{
free(copy[i]);
i++;
}
free(copy);
}
我在退出
之前释放了这个函数内的副本void ft_process_cmd(t_cmd *cmd, char **env, struct termios *t, t_stack *hist)
{
if (cmd->user_comm != NULL)
{
if (ft_strequ(cmd->user_comm[0], "exit"))
{
free_stack(hist);
freecopy(env); //I free the copy here
free_cmd(cmd);
ft_close_keyboard(t);
exit(0);
}
hist->size = hist->hist_count;
ft_push(hist, cmd->get_line);
ft_run_commands(cmd->user_comm, cmd->get_line, env, *hist);
}
}
Valgrind结果
==7699== HEAP SUMMARY:
==7699== in use at exit: 15,511 bytes in 26 blocks
==7699== total heap usage: 1,404 allocs, 1,391 frees, 120,569 bytes allocated
==7699==
==7699== 51 bytes in 2 blocks are definitely lost in loss record 8 of 19
==7699== at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==7699== by 0x403C81: ft_memalloc (in /home/julekgwa/21shell/21sh)
==7699== by 0x4039E9: ft_strdup (in /home/julekgwa/21shell/21sh)
==7699== by 0x4015EE: envp_cpy (ft_ctrl_c_signal_handler.c:74)
==7699== by 0x401050: main (main.c:107)
==7699==
==7699== 96 bytes in 4 blocks are definitely lost in loss record 12 of 19
==7699== at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==7699== by 0x403C81: ft_memalloc (in /home/julekgwa/21shell/21sh)
==7699== by 0x4039E9: ft_strdup (in /home/julekgwa/21shell/21sh)
==7699== by 0x401CB8: ft_add_value (ft_env.c:27)
==7699== by 0x401DF8: ft_setenv (ft_env.c:54)
==7699== by 0x401854: ft_modpwd (ft_cd.c:82)
==7699== by 0x4019BD: ft_cd (ft_cd.c:113)
==7699== by 0x400CE1: ft_execute_commands (main.c:41)
==7699== by 0x4026EE: ft_advanced_com (ft_more_utils.c:68)
==7699== by 0x400E8F: ft_run_commands (main.c:65)
==7699== by 0x4013E3: ft_process_cmd (ft_commands.c:86)
==7699== by 0x4010D9: main (main.c:117)
==7699==
==7699== 108 bytes in 4 blocks are definitely lost in loss record 13 of 19
==7699== at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==7699== by 0x403C81: ft_memalloc (in /home/julekgwa/21shell/21sh)
==7699== by 0x4039E9: ft_strdup (in /home/julekgwa/21shell/21sh)
==7699== by 0x401CB8: ft_add_value (ft_env.c:27)
==7699== by 0x401DF8: ft_setenv (ft_env.c:54)
==7699== by 0x401837: ft_modpwd (ft_cd.c:81)
==7699== by 0x4019BD: ft_cd (ft_cd.c:113)
==7699== by 0x400CE1: ft_execute_commands (main.c:41)
==7699== by 0x4026EE: ft_advanced_com (ft_more_utils.c:68)
==7699== by 0x400E8F: ft_run_commands (main.c:65)
==7699== by 0x4013E3: ft_process_cmd (ft_commands.c:86)
==7699== by 0x4010D9: main (main.c:117)
==7699==
==7699== LEAK SUMMARY:
==7699== definitely lost: 255 bytes in 10 blocks
==7699== indirectly lost: 0 bytes in 0 blocks
==7699== possibly lost: 0 bytes in 0 blocks
==7699== still reachable: 15,256 bytes in 16 blocks
==7699== suppressed: 0 bytes in 0 blocks
==7699== Reachable blocks (those to which a pointer was found) are not shown.
==7699== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==7699==
==7699== For counts of detected and suppressed errors, rerun with: -v
==7699== Use --track-origins=yes to see where uninitialised values come from
==7699== ERROR SUMMARY: 17 errors from 6 contexts (suppressed: 0 from 0)