Link to home
Start Free TrialLog in
Avatar of cleaverX
cleaverX

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cleaverX
cleaverX

ASKER

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


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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> 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



@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.
>>>> 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)
{
  if(problem)
  {
   this->Close();
   Application->Terminate();  
   return;
  }
}

Open in new window

Hi,
I simply split the points , thanks for the help everyone.
greetz
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