在子进程中使用std :: string时出现C ++内存泄漏

时间:2013-03-27 22:37:21

标签: c++ string memory-leaks fork

我遇到这段代码的问题,valgrind检测到std :: basic_string中的内存泄漏,我不知道我做错了什么。只有在子进程中使用std :: string时才会泄漏。你能告诉我,问题在哪里吗?我之前从未在C ++中使用过fork(),因此我没有多少经验。

#include <iostream>
#include <string>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t childPid = fork();
    if (childPid == -1)
    {
      perror("fork");
      return EXIT_FAILURE;
    }
    else if (childPid == 0)
    {
      std::cout << "Child PID: " << getpid() << std::endl;
      std::string str("something");      //valgrind detects mem leak here
      _Exit(EXIT_SUCCESS);
    }
    else
    {
      //std::string str("something");    //but not here
    }

    waitpid(-1, 0, 0);
  return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:9)

_Exit不会运行任何析构函数或atexit函数,它只会立即结束。

显然,这通过RAII打了一个巨大的洞,所以不要这样做。


在保持相同退出策略的同时实施“不要这样做”的一种方法可能是:

template <typename Func, typename... Args>
int wrap_cpp_code(Func&& func, Args&&.. args)
{
    try
    {
        return std::forward<Func>(func)(std::forward<Args>(args)...);
    }
    catch (...)
    {
        return EXIT_FAILURE;
    }
}

在其范围内的所有析构函数运行之前,不会咳出返回值,并给出:

int child_main(int argc, char *argv[])
{
    std::cout << "Child PID: " << getpid() << std::endl;
    std::string str("something");

    return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
    pid_t childPid = fork();
    if (childPid == -1)
    {
      perror("fork");

      return EXIT_FAILURE;
    }
    else if (childPid == 0)
    {
      int ret = wrap_cpp_code(child_main, argc, argv);
      _Exit(ret);
    }
    else
    {
      /*
      int ret = wrap_cpp_code(xyz, argc, argv);
      */
    }

    waitpid(-1, 0, 0);

    return EXIT_SUCCESS;
}

但是仍然没有考虑atexit函数或全局析构函数。所以仍然避免这样做。

相关问题