delphi 动态创建PANEL,销毁时报错!

panel出现在不同的窗体上。

调用代码:

CreatePnlMess(ProcureForm,380,1,'无需重复!');

FORM1调用,如果PANEL还没有消失的话,Form2在调用PANEL会出现,但是2秒后会报错。

可能是计时器销毁的时候报错了!

创建时的代码

  if Frm.FindComponent(Frm.Name + '_PnlMess') <> nil then begin
    exit;
  end else begin
    pnl := TPanel.Create(Frm);
    with pnl do begin
      Name := Frm.Name + '_PnlMess';
      Parent := Frm;
      Height := 70;
      Width := Wid;
      ParentBackground := False;
      Color := $00F0CAA6;
      Left := (Frm.Width - pnl.Width) div 2;
      Top := (Frm.Height - pnl.Height) div 2;
      BringToFront;
      Caption := '';
    end;
    lbl := TLabel.Create(pnl);
    with lbl do begin
      //Name := Frm.Name + 'LblMess';
      Parent := pnl;
      Caption := Text;
      Left := (pnl.Width - lbl.Canvas.TextWidth(lbl.Caption)) div 2;
      Top := (pnl.Height - Height) div 2;
      AutoSize := True;
      BringToFront;

      Font.Size := 10;
      Font.Style := [fsBold];
    end;

    Ico := TIcon.Create;
    MainForm.ilMess.GetIcon(iL, Ico);

    img := TImage.Create(pnl);
    with img do begin
      //Name := Frm.Name + 'ImgMess';
      Parent := pnl;
      Height := 32;
      Width := 32;
      Left := 25;
      Top := (pnl.Height - Height) div 2;
      BringToFront;
      Picture.Assign(Ico);
    end;

    Trm := TTimer.Create(Frm);
    with Trm do begin
      //Name := Frm.Name + 'TrmMess';
      Interval := 2000;
      OnTimer := MainForm.CurrentTimer;
      Enabled := True;
    end;
  end;

倒计时销毁的代码

  img.Free;
  Ico.Free;
  lbl.Free;
  Trm.Free;
  pnl.Free;

你在哪里销毁的,是不是出现了重复释放,也就是同一个变量已经释放再次释放的情况
先把所有的free的代码全部注释,然后一条一条添加上去,看到哪里报错。

不应该在主窗体里释放,识别时机和识别对象都有不确定性;应该是各个拥有panel的窗体自己负责panel的释放