I'm having problem with this piece of code, valgrind detects memory leaks in std::basic_string and I don't know what am I doing wrong. The leak is only when std::string is used in child process. Could you please tell me, where the problem is? I've never used fork() in C++ before so I don't have much exprience.
#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; }
_Exittry returning like you do at the end of the function.