0

I'm looking for a working example of using fork() within a newly created pthread, where the child runs a process using execvp() & the parent thread waits for the child. The program must close without memory leaks.

I am close to a solution myself however I've run into a memory leak (valgrind) because the child process leaves behind memory from the fork'

  • And where exactly did valgrind tell you the memory leak came from? You do understand that, upon execvp, the original process gets unceremoneously replaced by the new process, without going through such trivialities as deallocating all allocated memory. – Sam Varshavchik Oct 09 '20 at 02:05
  • The memory leak arises from the parent objects. I'm calling fork and exec from within a class, and that class isn't freed correctly on child pass – Śaeun acreáť Oct 09 '20 at 02:07
  • Regarding your hunt for an example, are all the internet search engines offline? Regarding your solution with problems, if you are wanting help then you should show what you have. – paddy Oct 09 '20 at 02:07
  • Please reread the second half of my previous comment. There are no memory leaks in the described scenario, this is a false positive from valgrind. – Sam Varshavchik Oct 09 '20 at 02:09
  • The memory leak is quite like this example however within a thread https://stackoverflow.com/questions/42143707/valgrind-trace-children-yes-reports-leak-despite-atexit-cleanup – Śaeun acreáť Oct 09 '20 at 02:18
  • Trust me, I've been searching rapidó – Śaeun acreáť Oct 09 '20 at 02:19
  • As I'm calling execvp which will replace memory in the forked process I can use child silent after fork to suppress the valgrind issue and the main process reports 0 leaks. – Śaeun acreáť Oct 09 '20 at 02:27

1 Answers1

0

I resolved the memory leak after some time searching through possible causes. I found the issue was execvp was returning -1 indicating the command wasn't being called correctly and therefore not replacing the child process stack correctly.

After ensuring the correct command was passed the leak was resolved.