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

Hi,

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)
{
  if(problem)
  {
      this->Close(); //or Application->Terminate(); same effect
      return;
  }
}
 
void __fastcall TfmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
   delete [] someMemory;
}

Open in new window

cleaverXAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
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

     PostMessage(WM_CLOSE);

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

0
 
mrwad99Connect With a Mentor Commented:
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().

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


if(problem)
{
   this->Close();
   Application->Terminate();  
   return;
}

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
void_mainConnect With a Mentor Commented:
Hello!
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.

void_main
0
 
itsmeandnobodyelseCommented:
>>>> Why do you use a TForm at all?

Hi void_main, shouldn't it be int_main? Is this really neccessary?
0
 
void_mainCommented:
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  ;) )
0
 
itsmeandnobodyelseCommented:
>>>> 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



0
 
void_mainCommented:
@itsmeandnobodyelse
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.
0
 
itsmeandnobodyelseCommented:
>>>> 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).  
0
 
cleaverXAuthor Commented:
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)
{
  if(problem)
  {
   this->Close();
   Application->Terminate();  
   return;
  }
}

Open in new window

0
 
cleaverXAuthor Commented:
Hi,
I simply split the points , thanks for the help everyone.
greetz
0
 
mrwad99Commented:
Cool, glad you got it working :o)
0
 
void_mainCommented:
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
0
All Courses

From novice to tech pro — start learning today.