ending a program early

Hi guys,

I'm doing some moderately intensive numerical simulation in C++ on Ubuntu. When I test new boundary conditions/parameters I often see from the streaming output that things have gone awry. I've been ending the program with Ctrl-C Ctrl-C, but I think that is causing memory leaks (seems to make sense, as the destructors probably don't get called). My question is: will this indeed cause a mem leak? What is a better way to stop the program? I.e., make it "listen" for a break key?

Andy
No, it won't cause memory leaks. The Operating System always keeps track of all the memory a program uses and will free it once the program is ended, no matter what.
I'm pretty sure that is false. I've definitely had people say otherwise before. And besides, I observe that it does!
You can get signals and call a function which does the cleaning, see http://www.cplusplus.com/reference/clibrary/csignal/signal/
The Ctrl-C signal should be SIGINT
Ideally JoR is right, the part of the operating system that is in charge of assigning resources is the scheduler\dispatcher. If memory that was allocated to a process is still marked as occupied then the OS's Garbage Collector is responsible for fixing that. "Memory Leaks" occur usually during the run time of the process, like when you are dynamically allocating RAM to a static scope in a function and you don't clear it. Eventually that allocated RAM builds up and you force something to "Page Out".
I've been ending the program with Ctrl-C Ctrl-C...

Is this a typo? Or do you actually need to hit Ctrl-C twice? If the later is the case then I'd say you're using Code::Blocks. The reason C::B doesn't append system("pause"); or the Linux equivalent to the end of your program like wxDev-C++ does, yet it has the ability to hold the consule open, is that your code is being executed within a shell that it creates when you run it. If you really are experiancing a memory leak, and that is a big if, then kill the IDE and restart it see if that fixes your issue.
Last edited on
Well, amccorm, afaik every reserved memory HAS to have a process that owns it so depending on what your program does, it might cause other processes or the operating system to allocate unnecessary memory but for everything statically allocated on the stack, and dynamically allocated on the heap using 'new' or 'malloc', I'm pretty sure it will be freed when the process ends.
Last edited on
You can never be sure about that.
If you want your resources freed you have to free them yourself.
Having memory leaks is bad even if your OS will reclaim the memory.
If your OS has virtual memory management, and I would guess Ubuntu does, then any memory that is used by your program exclusively (is not shared with other programs) will be reclaimed. The reason for this is that the OS supplies your program using a different higher level allocator that doesn't even discriminate between the stack, heap and static storage. When the program terminates, all is wiped out. However, if you are managing resources other than memory or as JoR said, communicate with other running processes, then memory may be lost.

PS: Win32 looses resources on a general basis. The OS has its own pool of handles for files, communication objects, etc. Those pools are not always managed effectively.
JoR's original reply pretty much sums it up. There is no harm in using Ctrl+C to force terminate your application.

Edit: unless you have buffers open that need to be flushed.
Last edited on
Topic archived. No new replies allowed.