We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


form->close() not working in formcreate() event or constructor

cleaverX asked
Medium Priority
Last Modified: 2012-05-06

I have an autocreated form, and in the ::FormCreate event or the form's constructor I'm reading some registry values.  If some values are missing, I try to quit the application.  I'm doing this by calling this->Close();
This calls the FormClose event (where I free all allocated memory).  However, the program does not quit.  I tried to put all the code in the constructor instead of in the FormCreate event, but it's the same.

When I close the program manually, the onClose event is called again, freeing memory that's already freed.  Question is why doesn't the application quit
__fastcall TfmMain::TfmMain(TComponent* Owner)
        : TForm(Owner)
      this->Close(); //or Application->Terminate(); same effect
void __fastcall TfmMain::FormClose(TObject *Sender, TCloseAction &Action)
   delete [] someMemory;

Open in new window

Watch Question

I don't know C++ Builder but I would assume that the Close member function can only called as a response to a WM_CLOSE message sent to the form dialog (then the onClose was invoked).

You could try to send the WM_CLOSE programmatically by calling


If that doesn't compile you should look fro a similar named member function of TForm (maybe postMessage with a small letter).

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
I would personally not try and quit from the constructor.  Instead, I would throw an exception from the constructor, which, when caught, calls TfMain::Close(), or, later on, call some special function that verifies whether or not all values were read from the registry.  If all values were not read, then you can again call Close().



looks like the code below is working fine:
oncreate is interrupted, application quits + destructor is called:


Why do you use a TForm at all? Is this really neccessary?
My suggestion is -if you don't need a TForm or something to display- you do the following:
- Start BCB
- Select "New" -> "More" (Maybe the words are not correct. I have a german BCB version!!!)
- In the popping up window you select "console expert"
- In the next window you chose what you like but leave "console application" unchecked!
This creates your new project with the WinMain function:

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    return 0;

...where you put your code in.
Assembler works fine here and you can always exit with
    return <value>;
If you still need a TForm, select "using VCL" in the console-expert dialog and create your TForm with its constructor (But that'll be another topic).

according to the other posts you can call exit(0); to quit your program.

>>>> Why do you use a TForm at all?

Hi void_main, shouldn't it be int_main? Is this really neccessary?
Hi itsmeandnobodyelse...

Whats the sense of your reply? In my opinion (and in the opinion of this community) we should not tease each other, but answer cleaverX's question. Riight???
By the way: Why int main when you dont need the return value? void_main sounds even better then int_main ;)

Is this question solved already? It seems the author has found his own solution anyway...

void_main (not int_main  ;) )
>>>> Whats the sense of your reply?

Your answer 'Why do you use a TForm at all? Is this really neccessary?' did invoke me to the reply.

TForm is the main class of a CPP Builder GUI application. If someone went that way to build an application by means of a comfortable and might class library it is rarely an option to tell him to simply drop the TForm. If you would have told him to using Perl instead of C++ the answer would have been similar strange and less helpful.

>>>> Why int main when you dont need the return value?
The standard says main should return an int. It should be 0 on success and non-zero if not successful. You can access the exit code by the calling program. Even in batch jobs you could do like

REM execute program

myapp arguments
if errorlevel 1 goto error

You're right!
But when you grew up with TForm you'll might find it hard not to use it. But BCB provides much more than TForms and GUI-Based applications...
By the way -> Isn't TApplication the main class in BCB instead of TForm? TForm does all the visual work but TApp... controls all the forms.


But what I wanted to say is:
Can't you read your registry-values (and abort programm if neccessary) before you create your TfmMain, cleaverX? Or is it essential for you to read your reg-values inside the TForm???

Anyway: The problem may already been solved by the autor himself.
>>>> But when you grew up with TForm you'll might find it hard not to use it. But BCB provides much more than TForms and GUI-Based applications...

I don't know much of BCB and I would like to apologize for my comment above. I assumed you wanted to tease CleverX with your comment and it seems I was wrong with that (though I still am convinced that a solution not using TForm makes no sense here).  


Hi Everyone,

sorry for my late reply, I was out of office for a while, and afterwards wasn't receiving email notifications anymore, so I forgot the topic.
Thanks for all the contributions everyone.  As was mentioned, the attached code solves the problem.
Not using TForm might have been an option because it was a totally new project, but as suggested here, once you're used to some way of working, you stick to it.
About the question of TApplication and TForm :
quote from the help file:  "The global variable Application, of type TApplication, is in every VCL- or CLX-based application. "
The owner of each form is that TApplication Object.

void __fastcall TfmMain::FormCreate(TObject *Sender)

Open in new window


I simply split the points , thanks for the help everyone.

Cool, glad you got it working :o)
Quote of itsmeandnobodyelse:
"[...] I assumed you wanted to tease CleverX with your comment [...]"

No!!!!!! That was never my intention!!! I just wanted to ask if he really needed the TForm. Maybe it sounded different from what it meant. Please excuse my misspells and gramma faults, because my first language is german!

I'm also glad you got it working. And tanks for the points, too
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.