Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-10

Where is form instance being NIL'ed

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

When a new form is created using Application.CreateForm(), the form's global instance variable is passed in as well. Now if that form's CloseAction = caFree, where is that instance variable assigned nil after it was freed?

In fpGUI, the form is freed, but the global instance variable is still pointing to a now non-existing form. I need that instance variable to be nil.

Any help you guys can offer?

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 6 lines
Hi, When a new form is created using Application.CreateForm(), the form's global instance variable is passed in as well. Now if that form's CloseAction = caFree, where is that instance variable assigned nil after it was freed?

It is not, unless you set it to nil in the BeforeDestroy event.

Using that variable is a very bad idea anyway.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 8 lines
> When a new form is created using Application.CreateForm(), the form's > global instance variable is passed in as well. Now if that form's > CloseAction = caFree, where is that instance variable assigned nil > after it was freed? It is not, unless you set it to nil in the BeforeDestroy event. Using that variable is a very bad idea anyway.

I hate those global form variables as well, but how else do you handle non-modal forms? For example. I create a non-modal TaxCodeListing form. The user used the main form's menu to get to TaxCodeList. If the form already exists, it should bring it to the front and not create another instance. To do that, if check if the global form variable = nil or not. After the first create and even if the user actually closed the form (not just sitting in the background), that global form variable in now always nil.

So when the user selects the menu option to view the TaxCodeList form a little later. The apps sees that the global form variable nil and tries to call .ActivateWindow() and I get a lovely exception error. :-(

function DisplayTaxCodeList: Boolean;
begin
  Result := False;
  if not Assigned(TaxCodeListForm) then
    fpgApplication.CreateForm(TTaxCodeListForm, TaxCodeListForm)
  else
    TaxCodeListForm.ActivateWindow;
  TaxCodeListForm.Show;
  Result := True;
end;

Any better suggestions for managing non-modal forms?

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: On Tue, Oct 14, 2008 at 3:50 PM, Michael Van Canneyt I hate those global form variables…
Graeme Geldenhuys wrote 38 lines
On Tue, Oct 14, 2008 at 3:50 PM, Michael Van Canneyt wrote: >> When a new form is created using Application.CreateForm(), the form's >> global instance variable is passed in as well. Now if that form's >> CloseAction = caFree, where is that instance variable assigned nil >> after it was freed? > > It is not, unless you set it to nil in the BeforeDestroy event. > > Using that variable is a very bad idea anyway. I hate those global form variables as well, but how else do you handle non-modal forms? For example. I create a non-modal TaxCodeListing form. The user used the main form's menu to get to TaxCodeList. If the form already exists, it should bring it to the front and not create another instance. To do that, if check if the global form variable = nil or not. After the first create and even if the user actually closed the form (not just sitting in the background), that global form variable in now always nil. So when the user selects the menu option to view the TaxCodeList form a little later. The apps sees that the global form variable nil and tries to call .ActivateWindow() and I get a lovely exception error. :-( function DisplayTaxCodeList: Boolean; begin Result := False; if not Assigned(TaxCodeListForm) then fpgApplication.CreateForm(TTaxCodeListForm, TaxCodeListForm) else TaxCodeListForm.ActivateWindow; TaxCodeListForm.Show; Result := True; end; Any better suggestions for managing non-modal forms?

Sure. All forms are owned by the global application, so:

C:=fpgApplication.FindComponent('TaxCodeListForm'); If (CNil) then
TaxCodeListForm(C).ActivateWindow
else
fpgApplication.CreateForm(TTaxCodeListForm, TaxCodeListForm)

Michael.

Reply by email

Graeme Geldenhuys
to Mattias Gärtner: An embedded and charset-unspecified text was scrubbed... Name: not available Url:…
Mattias G?rtner wrote 5 lines
The variable pointer is not stored, so can not be set to nil by the LCL. Afaik it is the same with the VCL. Why not set it to nil in TForm.Destroy?

That ties the class to a external instance variable. I thought there might be a more elegant way of handling this. But if there isn't, then that's what I'll have to do. :)

Is that the recommended method of handling non-modal forms in Delphi as well?

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Marc Weustink
to Graeme Geldenhuys
Graeme Geldenhuys wrote 12 lines
On Tue, Oct 14, 2008 at 4:20 PM, Mattias G?rtner wrote: > The variable pointer is not stored, so can not be set to nil by the LCL. Afaik > it is the same with the VCL. > > Why not set it to nil in TForm.Destroy? That ties the class to a external instance variable. I thought there might be a more elegant way of handling this. But if there isn't, then that's what I'll have to do. :) Is that the recommended method of handling non-modal forms in Delphi as well?

In Delphi I manage nonmodal forms myself in the mainform (and set them to in in Form.onDestroy). I don't use those global ones and remove them.

Marc

Reply by email