0

Form1 connects to a database and keeps the connection in a published property Form1->DBSession.

In the project source I would like to make sure that this session is closed, even if the program is aborted throwing an exception (in a library).

My code includes the form using

USEFORM("fForm1.cpp", Form1); 

When writing this code in WinMain

try {
   Application->Initialize();
   Application->CreateForm( __classid(TForm1), &Form1 );
   Application->Run();
} 
__finally
  {  Form1->DBSession->Close(); } 

the compiler does not compile as it does not know TForm1. I cannot include the header file as I need to use USEFORM and get a redeclaration error including.

Ralph M. Rickenbach
  • 12,893
  • 5
  • 29
  • 49
  • How is abort() being called? Could you choose to not use abort and use exit() and atexit or throw exit_exception(exit_code); instead? This SO question may be of some assistance: http://stackoverflow.com/questions/397075/what-is-the-difference-between-exit-and-abort – David Dean Dec 08 '09 at 18:55
  • My problem is that I have to call a form property or a form method in the project source. I cannot control the code that exits the run method, but I find myself in the position that I have to make sure that under most circumstances the Form1->DBSession->close() method is called. – Ralph M. Rickenbach Dec 08 '09 at 22:43

1 Answers1

1

You do not need to add a try..finally block inside of WinMain(). After WinMain() exits, all active TForm objects are automatically freed. Simply have your MainForm close the DBSession inside its destructor.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770