Force C++Builder to close a database session even if aborted
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开发者_JS百科 the header file as I need to use USEFORM and get a redeclaration error including.
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.
精彩评论