Board index » cppbuilder » destruction of closed forms

destruction of closed forms


2007-05-24 12:45:41 AM
cppbuilder12
Hi all!
I don't know if I should post here or in vcl.using. My problem is this:
I noticed that when you close the main form, or terminate the
application, all the opened forms at that moment are freed when WinMain
ends running (example:
//-------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TMainForm), &MainForm);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
ManagersDestructor::destroy(); //something else... doesn't matter
GarbageCollector::destroy(); //here it tests for memory leaks
return 0;
}
My question is: let's say I have several forms opened and I close the
main form, causing the application to terminate... How can I manage to
impose some order to object destruction? (I want GarbageCollector to be
the last one being freed) ( I mean something different than tracking
which forms are open and close them one by one when the main form closes)
Thanks
 
 

Re:destruction of closed forms

"Darío Alejandro Guzik" < XXXX@XXXXX.COM >wrote in message
Quote
I noticed that when you close the main form, or terminate
the application, all the opened forms at that moment are
freed when WinMain ends running (example:
TApplication is a global object. All active forms that are owned by
TApplication are freeed when TApplication itself is freed. That
happens during the RTL cleanup when the process is being terminated,
which is after WinMain() has exited.
Quote
I want GarbageCollector to be the last one being freed ( I mean
something different than tracking which forms are open and close
them one by one when the main form closes)
You can make GarbageCollection be a global object as well, and use
#pragma exit to register a function that frees the instance at process
shutdown after the VCL has finished its own cleanup.
An alternative is to make GarbageCollector be reference-counted.
WinMain() and the MainForm can both increment and decrement the
reference count separately. The GarbageCollector instance will not
actually be freed until the reference count falls to 0. In this case,
when the MainForm is freed, as the reference count will be 1 after
WinMain() has exited, so the instance is still alive.
Gambit
 

Re:destruction of closed forms

Hi Remy!
looking carefully at my code (well, is not really mine as you will
understand) I found out these facts:
1- There are some objects in some forms that are tracked by my garbage
collector
2- Forms that were open when the main form closes are not being
"closed", meaning that the onClose event is not ever called.
3- The function I use to check memory leaks has a template... could it
be used with pragma exit?
4- I'm now calling atexit() for tha function. Accorging to the
documentation the calling order at program exit is:
"All global objects are active until the code in all exit procedures has
executed. Local variables, including those declared in the main
function, are destroyed as they go out of scope. The order of execution
at the end of a C++Builder program is as follows:
atexit() functions are executed in the order they were inserted.
#pragma exit functions are executed in the order of their priority codes.
Destructors for global variables are called."
so if I cannot destroy myself the objects, using atexit or #pragma exit
would not solve my problem since the form is a global variable.
Perhaps I'm doing it the wrong way, but this memory leaks test are not
called from within the garbaje collector: I have a template class where
I add pointers to the objects I create, and delete the pointers when the
objects are deleted. When the application closes, and the GC does the
final free operation, if any pointers are still in these classes with
ref.count greater than 0, then it's a leak, so the check function is
called from that classes.
But perhaps I'm going too deep and the solution is simpler: I want to
destroy all forms when someone closes the main form, so all my
destructors are called *before* atexit or #pragma exit is executed.
Remy Lebeau (TeamB) wrote:
Quote

TApplication is a global object. All active forms that are owned by
TApplication are freeed when TApplication itself is freed. That
happens during the RTL cleanup when the process is being terminated,
which is after WinMain() has exited.


You can make GarbageCollection be a global object as well, and use
#pragma exit to register a function that frees the instance at process
shutdown after the VCL has finished its own cleanup.

An alternative is to make GarbageCollector be reference-counted.
WinMain() and the MainForm can both increment and decrement the
reference count separately. The GarbageCollector instance will not
actually be freed until the reference count falls to 0. In this case,
when the MainForm is freed, as the reference count will be 1 after
WinMain() has exited, so the instance is still alive.


Gambit


 

{smallsort}

Re:destruction of closed forms

"Darío Alejandro Guzik" < XXXX@XXXXX.COM >wrote in message
Quote
Forms that were open when the main form closes are not being
"closed", meaning that the onClose event is not ever called.
It is not supposed to be. The application is being fully terminated.
All active form's are destroyed without asking for permission. The
OnClose event is only triggered when a form is being closed directly
by the user, or a system shutdown, allowing the code to decide whether
or not to actually allow the close. It can't do that when the
application is in the process of shutting down.
Gambit
 

Re:destruction of closed forms

Dear,
sorry if I write here, but I think I need your help.
Other times you solved my problems.
I posted a message on the ide newsgroup about DataBreakpoints
but no one answer me.
What should I do?
Thanks for your help,
Mauro.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote

"Darío Alejandro Guzik" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Forms that were open when the main form closes are not being
>"closed", meaning that the onClose event is not ever called.

It is not supposed to be. The application is being fully terminated.
All active form's are destroyed without asking for permission. The
OnClose event is only triggered when a form is being closed directly
by the user, or a system shutdown, allowing the code to decide whether
or not to actually allow the close. It can't do that when the
application is in the process of shutting down.


Gambit


 

Re:destruction of closed forms

mauro wrote:
Quote
I posted a message on the ide newsgroup about DataBreakpoints
but no one answer me.
What should I do?
1) Wait.
It's only been 2 hours. And it's the middle of the night.
2) Explain (in the ide group) what your problem actually is.
Asking if there are known bugs won't help you if those known bugs
don't affect what you are actually trying to do.
 

Re:destruction of closed forms

"mauro" < XXXX@XXXXX.COM >wrote in message
Quote
sorry if I write here, but I think I need your help.
Other times you solved my problems.
Please start a new discussion thread. Your questions have nothing to
do with this thread.
Gambit
 

Re:destruction of closed forms

Dear,
sorry, this is the last message I will write in this group.
You can note in the IDE group that it is 2 days that I wrote already a
message.
What you have seen is the last one where I tried to re-begin with a shorter
question.
I will answer this same words in the IDE group, on the older message.
Sorry for the night. I am in Italy :)
Thanks for your help.
"Bob Gonder" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
mauro wrote:

>I posted a message on the ide newsgroup about DataBreakpoints
>but no one answer me.
>What should I do?

1) Wait.
It's only been 2 hours. And it's the middle of the night.

2) Explain (in the ide group) what your problem actually is.
Asking if there are known bugs won't help you if those known bugs
don't affect what you are actually trying to do.



 

Re:destruction of closed forms

Yes I know, so I am very sorry.
I already began a thread in the IDE group, but no one answer me since two
days :(
You can find it in
I am desperate :(
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote

"mauro" < XXXX@XXXXX.COM >wrote in message
news:4656db31$ XXXX@XXXXX.COM ...

>sorry if I write here, but I think I need your help.
>Other times you solved my problems.

Please start a new discussion thread. Your questions have nothing to
do with this thread.


Gambit


 

Re:destruction of closed forms

Remy Lebeau (TeamB) wrote:
Quote
"Darío Alejandro Guzik" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Forms that were open when the main form closes are not being
>"closed", meaning that the onClose event is not ever called.

It is not supposed to be. The application is being fully terminated.
All active form's are destroyed without asking for permission. The
OnClose event is only triggered when a form is being closed directly
by the user, or a system shutdown, allowing the code to decide whether
or not to actually allow the close. It can't do that when the
application is in the process of shutting down.


Ok, thanks.... It's clear now.
I changed my forms to be MDI now and when the main form closes, it
explicitly closes all its children.
Thank you very much
Dario