Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture CTRL+C to avoid mem leak

Status
Not open for further replies.

hansaplast

Programmer
Dec 17, 2001
61
0
0
NL
Please consider the folowing:
Code:
main ()
{
  MyClass *clss = new MyClass;

  //.. some code ..//

  delete clss;
}
When the programm runs and CTRL+C is pressed, clss is not deleted thus creating a leak.
How can I avoid this? I tried 'try' and 'catch' but that doesn't seem to work.

Whatever you do will be insignificant, but it is very important that you do it.
(Mahatma Gandhi)
 
Well, as you can terminate your code with Ctrl-C you could just as well release the memory when doing so.

Maybe you should look over your program termination.

It could also be of interrest to look at the 'atexit'-call.

Totte
Keep making it perfect and it will end up broken.
 
Ok. I did the folowing:
Code:
#include <stdlib.h>

void destruct_main(void)
{
  delete clss;
}

main ()
{
  MyClass *clss = new MyClass;
  atexit(destruct_main);

  //.. some code ..//

  delete clss;
}
Hitting CTR+C doesn't execute destruct_main.
The funny thing is that when I use MyClass in a local scope. Something simular happens:
Code:
MyClass::MyClass()
{
  MyClass2 *mc = new MyClass2;
}

MyClass::~MyClass()
{
  delete MyClass2;
}

main ()
{
  MyClass clss;

  //.. some code ..//
}
mc in MyClass is not destroyed when hitting CTRL+C

Whatever you do will be insignificant, but it is very important that you do it.
(Mahatma Gandhi)
 
Borlands documentation (ref: abort) says:
Code:
If you call abort anywhere in a program, no destructors are called, not even for variables with a global scope.
Somehow no exception is thrown when pressing CTRL+C and thus exception handlers do not apply.

So my thought was that CTR+C (or pressing the console's X in that matter) makes a call to terminate(). Using the set_terminate function should then do the trick.. However, this seems not the case... Terminate() isn't called :(

Is there any way to catch these events and go from there?
I could really use some help with this..

Whatever you do will be insignificant, but it is very important that you do it.
(Mahatma Gandhi)
 
Number one, I am assuming that you are working with a VCL application, as you are working with C++ Builder. So try looking at the TAppEvents object; it has an event handler for the app terminate event.
Second thing to consider, And correct me if I am wrong experts (I have been told that I was once or twice, but I did not believe it), but when a Windows Process terminates, it releases all memory allocated to it. The thing to worry about is any OS Objects allocated like Handles, Threads, Mutexes, Etc. that you have explicitly allocated. Those may leak under an abnormal termination.

The simplest solution is the best!
 
I wonder what kind of program that you do that can be terminated with Ctrl-C, i'm using BCB6 and none of my program can be terminated with that key combination.

Are you making text-mode programs?

Totte
Keep making it perfect and it will end up broken.
 
Yep.. Commandline tools since they are so easy and flexible in use. Perhaps my UNIX background has something to with it and maybe that too has something to do with cleaning up my own stuff instead of letting the garbage collector decide (no offence Prattaratt) :)

Salem, from the VC++ forum had the answer. The solution is setting the console control handler SetConsoleCtrlHandler. More info can be find here:
Discussion regarding this item in the VC++ forum is here:
Thanks everyone!

Whatever you do will be insignificant, but it is very important that you do it.
(Mahatma Gandhi)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top