No Memory leaks with exit(1) function call??? 😳 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

No Memory leaks with exit(1) function call??? 😳

I was testing a memory leak tool for Mac out today that I found here: [https://dr-rost.medium.com/detect-memory-leaks-on-macos-4cf257529aa] and I noticed that the following code 'test.cpp' #include <iostream> using namespace std; int main() { string *x = new string; *x = "hello"; cout << (*x)[0]; } I compile: gcc -lstdc++ test.cpp -o test.out Then I run the memory leak detector: leaks -atExit -- ./test.out | grep LEAK I of course get a memory leak: 1 (32 bytes) ROOT LEAK: 0x60000143d120 [32] But if I properly deallocate memory by adding `delete x;' then compile and run leak detector as before I get no memory leak, as expected. However, when I change 'delete x;' to 'exit(1);' (or if I add exit(1) after delete x;) I get no memory leak??? But how can this be? I haven't deallocated the memory, so why don't I get a memory leak?

2nd Jul 2022, 12:51 PM
Edward Finkelstein
Edward Finkelstein - avatar
3 Answers
0
「HAPPY TO HELP」 but what about when you JUST have exit(1) and no delete, does valgrind show a memory leak for that?
2nd Jul 2022, 7:47 PM
Edward Finkelstein
Edward Finkelstein - avatar
0
P.s i have tested it on a bit more complicated example and it works as expected, it’s just with exit that i am surprised.
2nd Jul 2022, 7:48 PM
Edward Finkelstein
Edward Finkelstein - avatar
0
For now, I think the tool seems to work as long as one doesn't use the exit function. I tested on this example https://code.sololearn.com/cXaPN6SEMo2u and commented out one or both of the deletes and the tool caught the memory leaks, but when I got rid of the deletes and used just exit(1) after cout then it did not catch it.
3rd Jul 2022, 11:15 AM
Edward Finkelstein
Edward Finkelstein - avatar