使用无效命令的fork导致valgrind中的内存泄漏 [英] fork with invalid command cause a memory leak in valgrind

查看:243
本文介绍了使用无效命令的fork导致valgrind中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,这些代码在fork中执行了无效命令. 以下代码返回valgrind中的内存泄漏.

I have the following code which execute a non valid command within a fork. The following code return a memory leak in valgrind.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>


int external_cmd(char **argv)
{
    int pid;

    if ((pid = fork()) == -1)
        return -1;

    if (pid == 0) {
        /* child */
        execvp(argv[0], argv);
        exit(0);

    } else if (pid < 0)
        return -1;

    int status;
    while (wait(&status) != pid);

    return 0;
}

int main ()
{
    char *argv[8] = {0};
    argv[0] = "tawtaw"; //<--------- invalid command
    argv[1] = "-a";

    char *mem = strdup("anychar");

    /* fork call */
    external_cmd(argv);

    free(mem);

   return(0);
}

使用valgrind return执行上述代码:

executing the above code with valgrind return:

$ valgrind --leak-check=full --show-leak-kinds=all ./test
==11573== Memcheck, a memory error detector
==11573== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11573== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==11573== Command: ./test
==11573== 
==11574== 
==11574== HEAP SUMMARY:
==11574==     in use at exit: 8 bytes in 1 blocks
==11574==   total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==11574== 
==11574== 8 bytes in 1 blocks are still reachable in loss record 1 of 1
==11574==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11574==    by 0x4EBF729: strdup (strdup.c:42)
==11574==    by 0x400747: main (in /home/mohamed/Desktop/tech/test/test)
==11574== 
==11574== LEAK SUMMARY:
==11574==    definitely lost: 0 bytes in 0 blocks
==11574==    indirectly lost: 0 bytes in 0 blocks
==11574==      possibly lost: 0 bytes in 0 blocks
==11574==    still reachable: 8 bytes in 1 blocks
==11574==         suppressed: 0 bytes in 0 blocks
==11574== 
==11574== For counts of detected and suppressed errors, rerun with: -v
==11574== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==11573== 
==11573== HEAP SUMMARY:
==11573==     in use at exit: 0 bytes in 0 blocks
==11573==   total heap usage: 1 allocs, 1 frees, 8 bytes allocated
==11573== 
==11573== All heap blocks were freed -- no leaks are possible
==11573== 
==11573== For counts of detected and suppressed errors, rerun with: -v
==11573== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

注意::如果我使用有效的命令"ls"代替"tawtaw"来执行代码,则流浪将不会返回内存泄漏.

NOTE: If I execute the code with a valid command "ls" in stead of "tawtaw" then valgring will not return memory leak.

我想念什么?

推荐答案

这是预期的.当execve()无法执行命令时,它将控制权返回给您的代码,然后退出并永远不会释放strdup()的内存.

That is expected. When execve() can't execute command, it returns control to your code, and than you exit and never free memory from strdup().

execve成功后,整个文件映像将被替换,并且strdup()分配的内存没有剩余.

When execve succeeds, the whole file image is replaced, and nothing remains of the memory allocated with strdup().

这篇关于使用无效命令的fork导致valgrind中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆