Form.release ou Action:=cafree? Qual usar?

Delphi

24/05/2004

Galera, em minha app, crio os forms dinamicamente e tenho tido dificuldades para lidar com esta situacao.

Ex:
//crio
try
frmconsulta:= Tfrmconsulta.create(application);
except
end;

// executo
frmconsulta.showmodal;
//destruo
frmconsulta.release;

Q dúvida é a seguinte, quando eu uso o release como acima, é necessário usar no evento Onclose do frmconsulta a linha:
Action:=cafree?

Ou o release faz a mesma coisa? Ou se eu usar o cafree e depois usar o release, isto pode acarretar erros?


Obrigado


Aledeol

Aledeol

Curtidas 1

Melhor post

Vinicius2k

Vinicius2k

24/05/2004

aledeol,

Vamos às definições :
[b:1c4b74a806]procedure Free;[/b:1c4b74a806] [b:1c4b74a806]Description[/b:1c4b74a806] Use Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an owner should be destroyed by a call to Free so that it can be properly disposed of and its memory released. Unlike Destroy, Free is successful even if the object is nil; so if the object was never initialized, Free won’t result in an error. When you call Free for a component, it calls Free for all components that it owns—that is, all components in its component list. Since a form owns all the controls and other components that are created on it in design mode, those components are automatically freed when the form is freed. By default, all forms are owned by the Application object; when the application terminates, it frees the Application object, which frees all forms. For objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with them; otherwise the allocated memory will not be usable until after the application terminates. Warning:Never explicitly free a component within one of its own event handlers or the event handler of a component it owns or contains. For example, don’t free a button, or the form that owns the button, in its OnClick event handler. To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.


[b:1c4b74a806]procedure Release;[/b:1c4b74a806] [b:1c4b74a806]Description[/b:1c4b74a806] Use Release to destroy the form and free its associated memory. Release does not destroy the form until all event handlers of the form and event handlers of components on the form have finished executing. Release also guarantees that all messages in the form´s event queue are processed before the form is released. Any event handlers for the form or its children should use Release instead of Free (Delphi) or delete (C++). Failing to do so can cause a memory access error. Note:Release returns immediately to the caller. It does not wait for the form to be freed before returning.

[b:1c4b74a806]caFree : [/b:1c4b74a806]The form is closed and all allocated memory for the form is freed. ...To close the form and free it in an OnClose event, set Action to caFree.


Existem várias sintaxes, possíveis para criação e liberação do form...
  // Para .ShowModal

  // 1º
  Form2:= TForm2.Create(Application);
  Form2.Showmodal;
  Form2.Free;
  Form2:= nil;

  // 2º
  Application.CreateForm(TForm2,Form2);
  Form2.Showmodal;
  Form2.Free;
  Form2:= nil;

  // 3º Free e :=nil pode ser substituido por FreeAndNil
  // nos dois exemplos...
  Form2:= TForm2.Create(Application);
  Form2.Showmodal;
  FreeAndNil(Form2);

  // 4º Release tbm pode ser usado...
  Form2:= TForm2.Create(Application);
  Form2.Showmodal;
  Form2.Release;
  Form2:= nil;

  // Para .Show vc precisa trabalhar a liberação no Form2...

  // 5º
  Form2:= TForm2.Create(Application);
  Form2.Show;
  { 1º
    procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Action:= caFree;
      Form2:= nil;
    end;

    2º
    procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Release;
      Form2:= nil;
    end; }


Estas são as formas que estou me lembrando agora... talvez exista mais alguma...
Se vc usar caFree seguido de := nil no Form2 e depois Free ou Release no Form chamador vc terá uma exceção pois o objeto já não existe mais e já foi nulado...
Espero ter ajudado...
T+


GOSTEI 1

Mais Respostas

Aledeol

Aledeol

24/05/2004

Blz,

ajudou bastante. Vou verificar o q fiz. O interessante é q criei um função de dentro de uma dll q Cria o frmconsulta. E toda vez q a função termina ele dá access violation.

E realmente eu usa cafree e depois release no form1. Pode ser isto.

Obrigado


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

24/05/2004

Colega,

No caso de MDI (raramente uso) eu utilizo caFree. No caso de aplicações SDI eu uso simplesmente free ou release, dependendo da garantia que eu necessite antes de liberar o formulário.


GOSTEI 0
Paulo_amorim

Paulo_amorim

24/05/2004

[quote:d64cab06d1=´Aroldo Zanela´]Colega,

No caso de MDI (raramente uso) eu utilizo caFree. No caso de aplicações SDI eu uso simplesmente free ou release, dependendo da garantia que eu necessite antes de liberar o formulário.[/quote:d64cab06d1]

Eu sempre utilizei Free em aplicações SDI, deixando o caFree para MDI (o qual tenho aversao eheh :roll:)

frm := Tfrm.Create(Self);
ShowModal;
frm.Free;


Isso garante a criação/liberação do form até onde eu sei...
Não sei se tem alguma limitação, mas...

Até+


GOSTEI 0
POSTAR