3
我遇到了这段代码的问题,valgrind在std :: basic_string中检测到内存泄漏,我不知道我做错了什么。只有在子进程中使用std :: string时才会发生泄漏。你能告诉我,问题在哪里?我从来没有在C++中使用fork(),所以我没有太多的经验。在子进程中使用std :: string时C++内存泄漏
#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;
}
而不是调用'_Exit'尝试像函数结束时那样返回。 – 2013-03-27 22:39:15
_Exit是否阻止堆栈退出?这可能会阻止std :: string析构函数的运行。 – Ben 2013-03-27 22:40:49