tags:

views:

81

answers:

1

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.

+1  A: 

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 - TeamB
Is this done too when I use Ctrl-F2 to stop execution in the development environment?
Ralph Rickenbach
Ctrl-F2 or calls to abort() will not call destructors nor ever reach a __finally block.
David Dean - Embarcadero

related questions