Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   memory leak em classe (https://www.clubdelphi.com/foros/showthread.php?t=73626)

sirmenon 05-05-2011 20:28:16

memory leak em classe
 
Olá amigos,
Estou com um problema de memory laeak e não estou conseguindo identifica-lo.
Segue o meu código:

Eu tenho a seguinte classe:
Código Delphi [-]
type 
  TFormParam = class 
  private 
    FCampos: string; 
    procedure SetCampos(const Value: string); 
  public 
    VinculaCampos : TStringList; 
    property Campos: string read FCampos write SetCampos; 
  Constructor Create; 
    destructor Destroy; 
  end; 
  
   ... 
  
constructor TFormParam.Create; 
begin 
  VinculaCampos := TStringList.Create;   
end; 
  
destructor TFormParam.Destroy; 
begin 
  VinculaCampos.Free; 
  inherited; 
end; 
  
procedure TFormParam.SetCampos(const Value: string); 
begin 
  FCampos := Value; 
  VinculaCampos.CommaText := Value; 
end;

Estou utilizando ela para passar informações de um Form para o outro, como no exemplo:

No Form1 eu chamo assim:
Código Delphi [-]
  
   objParam := TFormParam.Create; 
   try 
     objParam.Campos := 'Neil=45, Brian=63, Jim=22'; 
   if Form2 = nil then 
       Form2 := TForm2.Create(Self,objParam); 
     Form2.ShowModal; 
   finally 
     objParam.Free; 
   end;

No Form2 eu utilizo assim:
Código Delphi [-]
constructor TForm2.Create(Owner: TComponent; FrmParametro: TFormParam); 
var 
  i : Integer; 
begin 
  inherited Create(Owner); 
  for I := 0 to FrmParametro.VinculaCampos.Count - 1 do 
  begin 
    lstBox1.Items.Add(FrmParametro.VinculaCampos.Names[i]) ; 
    lstBox2.Items.Add(FrmParametro.VinculaCampos.ValueFromIndex[i]); 
  end; 
end;


Alguém saberia me dizer aonde esta o memory leak?

Casimiro Notevi 05-05-2011 22:12:08

¿Y cómo sabes que tienes un memory leak?, ¿cómo lo verificas?.


ecfisa 05-05-2011 22:32:39

Hola.

Otra pregunta, esta línea:
Código Delphi [-]
 
  Form2 := TForm2.Create(Self,objParam);

¿ Te la compila ?

Un saludo.

sirmenon 06-05-2011 14:21:19

Resposta 1:
Eu estou usando o EurecaLog, ele aponta um memory leak na criação de TStringList.

Resposta 2:
Sim, esta compilando normalmente.

JoseAntonio 06-05-2011 16:16:51

Código Delphi [-]
constructor TFormParam.Create; 
begin 
  VinculaCampos := TStringList.Create;
   
end;

gatosoft 06-05-2011 16:33:00

Probablemente hagan falta estas linea...

Código Delphi [-]
destructor Destroy;

Cambiar por:
Código Delphi [-]
destructor Destroy; Override


Código Delphi [-]
constructor TFormParam.Create; 
begin 
 VinculaCampos := TStringList.Create;   
end;

Cambiar por:

Código Delphi [-]
constructor TFormParam.Create; 
begin 
   inherited create;
  VinculaCampos := TStringList.Create;   
end;

¿Has hecho un seguimiento (Debug) paso a paso (F7) para saber la linea que te genera el error?

Saludos,

JoseAntonio 06-05-2011 16:42:59

una clase deberia tener las minimas interacciones con otras clases, esto es por una cuestion de orden, por eso yo normalmente utilizo formularios modales y funciones de clase y paso los parametros durante la creacion, ahi te envio un ejemplo para que lo modifiques al gusto

Código Delphi [-]
class function TfrmEditField.EditField(var AName, AType, ALong,
  ADisplay: string): boolean;
var
  AfrmEditfield: TfrmeditField;
begin
  try
    AfrmEditField := TfrmEditField.create(Application);
    AfrmEditField.lbNameField.Caption:= Aname;
    AfrmEditField.ComboTipo.text := AType;
    AfrmEditfield.DBLong.Text:= ALong;
    AfrmEditfield.DBDisplay.Text:= ADisplay;
    result := false;
    if AfrmEditField.ShowModal = mrOk then begin
      AType := AfrmEditField.combotipo.text;
      ALong := AfrmeditField.dblong.Text;
      Adisplay:= Afrmeditfield.DBDisplay.Text;
      result := true;
    end;
  finally
    AfrmEditField.free;
  end;

salu2

sirmenon 07-05-2011 17:19:31

Olá amigos,
Colocando o override resolveu.
Obrigado pelas dicas.


La franja horaria es GMT +2. Ahora son las 08:47:36.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi