内存释放问题(100分)

  • 主题发起人 主题发起人 yxz123
  • 开始时间 开始时间
Y

yxz123

Unregistered / Unconfirmed
GUEST, unregistred user!
写一个控件,动态创建的一个Button
constructor TASGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBtnTest:=TButton.Create(nil);
FBtnTest.parent:=TWincontrol(AOwner);
FBtnTest.Caption:='┅';
FBtnTest.OnClick:=BtnClick;
FBtnTest.BringToFront;//没有这句就不会出错
end;
destructor TASGrid.Destroy;
begin
if Assigned(FBtnTest) then
FBtnTest.Free;//报错
if Assigned(FcbTest) then
FcbTest.Free;//报错
inherited Destroy;
end;
谁能告诉我原因,如何解决?
 
你没有为FBtnTest设置OWNER
 
如何设置?Owner为只读的呀?
我试过FBtnTest:=TButton.Create(AOwner);也不行
 
将按钮置前是什么意思,那句可以不要啊....我从来都不要那句,即:
constructor TASGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBtnTest:=TButton.Create(nil);
FBtnTest.parent:=TWincontrol(AOwner);
FBtnTest.Caption:='┅';
FBtnTest.OnClick:=BtnClick;
//将这里的一句删了
end;
destructor TASGrid.Destroy;
begin
if Assigned(FBtnTest) then
FBtnTest.Free;//报错
if Assigned(FcbTest) then
FcbTest.Free;//报错
inherited Destroy;
end;

 
我是从StringGrid中继承的,StringGrid做容器有问题,所以用
FBtnTest.parent:=TWincontrol(AOwner);
将FBtnTest创建在上一级中,并通过
FBtnTest.BringToFront;显示,不能上出FBtnTest.BringToFront这句
 
老大,将FBtnTest创建在上一级中,不是用
FBtnTest.BringToFront;来显示......
 
我这样测试了一下,没错!
看看是不是其它的原因?

type
TASGrid = class(TEdit)
private
FBtnTest:TButton;
procedure BtnClick(Sender: TObject);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
end;

procedure Register;

implementation

{ TASGrid }
constructor TASGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBtnTest:=TButton.Create(nil);
FBtnTest.parent:=TWincontrol(AOwner);
FBtnTest.Caption:='┅';
FBtnTest.OnClick:=BtnClick;
FBtnTest.BringToFront;//没有这句就不会出错
end;
destructor TASGrid.Destroy;
begin
if Assigned(FBtnTest) then
FBtnTest.Free;//报错
// if Assigned(FcbTest) then
// FcbTest.Free;//报错
inherited Destroy;
end;

procedure TASGrid.BtnClick(Sender: TObject);
begin
ShowMessage('OK');
end;

procedure Register;
begin
RegisterComponents('Standard', [TASGrid]);
end;
end.
 
我测试过没有问题啊。是不是你外面的代码有问题。
我测试的代码如下
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TASGrid = class(TControl)
public
FBtnTest:TButton;
constructor Create(AOwner: TComponent);
destructor Destroy; override;
published

end; //class..end
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
temp:TASGrid;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

constructor TASGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBtnTest := TButton.Create(nil);
FBtnTest.parent := TWincontrol(AOwner);
FBtnTest.Caption := '©©';
FBtnTest.SetBounds(100,100,20,20);
// FBtnTest.OnClick := Button1Click;
FBtnTest.BringToFront; //ûÓÐÕâ¾ä¾Í²»»á³ö´í
end;

destructor TASGrid.Destroy;
begin
if Assigned(FBtnTest) then
FBtnTest.Free; //±¨´í
inherited Destroy;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=TASGrid.Create(Self);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(temp);
end;

end.
 
其实你不用自己释放,create的时候指定owner,owner会帮你负责释放。
即FBtnTest:=TButton.Create(owner);
虚构函数就不用写了。
原因不知道。^_^
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TASGrid = class(TControl)
public
FBtnTest:TButton;
procedure Button3Click(Sender: TObject);
constructor Create(AOwner: TComponent);
destructor Destroy; override;
published

end; //class..end
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
temp:TASGrid;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

constructor TASGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBtnTest := TButton.Create(aowner);
FBtnTest.parent := TWincontrol(AOwner);
FBtnTest.Caption := '??';
FBtnTest.SetBounds(100,100,20,20);
FBtnTest.OnClick := Button3Click;
FBtnTest.BringToFront; //??óD?a???í2??á3?′í
end;

destructor TASGrid.Destroy;
begin
if Assigned(FBtnTest) then
FBtnTest.Free; //±¨′í
inherited Destroy;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=TASGrid.Create(Self);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(temp);
end;

procedure TASGrid.Button3Click(Sender: TObject);
begin
ShowMessage('btn');
end;

end.

一点问题也没有呀
 
我看不出具体什么原因?期待答案
 
建议你好好检查一下,要不把所有代码放上。
 
后退
顶部