TLabel释放出错

jjbhql 2009-07-03 12:09:17
我继承TCustomControl写了个控件,里面加了几个label控件,不知道为什么一到释放Label的时候就会出错?在别的单元里引用这个控件,运行时很正常,一到后边释放就蹦出错误'invalid pointer Operation'和‘Exception Einvalidpointer in model ...’
创建:
function CreateLable(LPoint: TPoint; LColor: TColor; LCaption: string):TLabel;
begin
Result :=TLabel.Create(nil);
Result.Font.Color := LColor;
Result.Font.Name :='宋体';
Result.Font.Height := -15;
Result.Font.Style :=[fsBold];
Result.Transparent :=True;
Result.Parent :=Self;
Result.Visible :=True;
Result.Left :=LPoint.X;
Result.Top :=LPoint.Y;
Result.Width :=40;
Result.Caption :=LCaption;
end ;

FLabLandPair := CreateLable(Point(196,64),clBlack,'0');
FLabIdlePair := CreateLable(Point(75, 64),clBlack,'0');
FLabLandKing := CreateLable(Point(317,64),clBlack,'0');
FLabIdleKing := CreateLable(Point(438,64),clBlack,'0');

=============================================================================
释放:
if Assigned(FLabLandPair) then FreeAndNil(FLabLandPair);
if Assigned(FLabIdlePair) then FreeAndNil(FLabIdlePair);
if Assigned(FLabLandKing) then FreeAndNil(FLabLandKing);
if Assigned(FLabIdleKing) then FreeAndNil(FLabIdleKing);
...全文
53 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
弘石 2009-07-03
  • 打赏
  • 举报
回复
这些代码没有问题,你是不是重复释放了?
haitao 2009-07-03
  • 打赏
  • 举报
回复
是不是你freeandnil的时候,label的一些基础(parent)已经先被释放了?
jjbhql 2009-07-03
  • 打赏
  • 举报
回复
简单的free也不行!第二个我也知道,只是我想知道上面到底为什么出错!
haitao 2009-07-03
  • 打赏
  • 举报
回复
简单Fxxx.free;行不行?
如果不是需要先于form而释放,可以Result :=TLabel.Create(Aform);{需要多传入一个参数aForm} 就不用人为释放了
jjbhql 2009-07-03
  • 打赏
  • 举报
回复
这是所有的Create和Destory,TPNGObject和 TPictureButtOn是第三方的并且已经用过多次肯定是没有问题的。
type
ResultType = (Idle, Ping, Land);
ResultTypeOption = (clNone,clLandPair,clIdlePair,clLandIdle);
CResultOption =packed record
arResultType: TBetPlace;
IsLandPair: Boolean;
IsIdlePair: Boolean;
end;


TableArrPos =packed record
X: Integer;
Y: Integer;
end;


constructor TBJLResultTable.Create(AOwer: TComponent);
var
I: ResultType;
J:ResultTypeOption;
PicPng: TPNGObject;
function CreateLable(LPoint: TPoint; LColor: TColor; LCaption: string):TLabel;
begin
Result :=TLabel.Create(Self);
Result.Font.Color := LColor;
Result.Font.Name :='宋体';
Result.Font.Height := -15;
Result.Font.Style :=[fsBold];
Result.Transparent :=True;
Result.Parent :=Self;
Result.Visible :=True;
Result.Left :=LPoint.X;
Result.Top :=LPoint.Y;
Result.Width :=40;
Result.Caption :=LCaption;
end ;
begin
inherited;
EmptyTable;

PicPng :=TPNGObject.Create;
PicPng.LoadFromResourceName(HInstance, 'TbaleBack');
FBackGround :=TPicture.Create;
FBackGround.Graphic :=PicPng;
Width :=FBackGround.Width;
Height :=FBackGround.Height;

for I :=Low(ResultType) to High(ResultType) do
begin
for J := Low(ResultTypeOption) to High(ResultTypeOption) do
begin
PicPng.LoadFromResourceName(HInstance, Format('%s%d',[FarrResName[I],ord(J)]));
FArrResultPic[I][J] := TPicture.Create;
FArrResultPic[I][J].Graphic :=PicPng;
end;

PicPng.LoadFromResourceName(HInstance,Format('%s',[FArrSResName[I]]));
FArrResultSPic[I] :=TPicture.Create;
FArrResultSPic[I].Graphic :=PicPng;

FLabResultNum[I] := CreateLable( Point(72+ord(I)*153, 42), FArrColor[I],'0');
FlabResultRate[I] := CreateLable( Point(102+ord(I)*153, 42), clBlack,'00.00%');
end;

FLabLandPair := CreateLable(Point(196,64),clBlack,'0');
FLabIdlePair := CreateLable(Point(75, 64),clBlack,'0');
FLabLandKing := CreateLable(Point(317,64),clBlack,'0');
FLabIdleKing := CreateLable(Point(438,64),clBlack,'0');

FCloseBtn :=TPictureButton.Create(Self);
FCloseBtn.Parent :=Self;
FCloseBtn.Visible := True;
FCloseBtn.Left :=475;
FCloseBtn.Top :=10;
FCloseBtn.OnClick := CloseBtnClick;

try
PicPng.LoadFromResourceName(Hinstance, 'CloseComm');
FCloseBtn.CommPicture.Graphic := PicPng;
PicPng.LoadFromResourceName(HInstance, 'CloseMove');
FCloseBtn.MovePicture.Graphic :=PicPng;
PicPng.LoadFromResourceName(HInstance,'CloseDown');
FCloseBtn.DownPicture.Graphic :=PicPng;
finally
if Assigned(PicPng) then
FreeAndNil(PicPng);
end;

end;


destructor TBJLResultTable.Destroy;
var
I:ResultType;
J:ResultTypeOption;
begin
inherited;

if Assigned(FLabLandPair) then FreeAndNil(FLabLandPair);
if Assigned(FLabIdlePair) then FreeAndNil(FLabIdlePair);
if Assigned(FLabLandKing) then FreeAndNil(FLabLandKing);
if Assigned(FLabIdleKing) then FreeAndNil(FLabIdleKing);
if Assigned(FCloseBtn) then FreeAndNil(FCloseBtn);

for I := Low(ResultType)to High(ResultType) do
begin
for J := Low(ResultTypeOption) to High(ResultTypeOption) do
begin
if Assigned(FArrResultPic[I][J]) then FreeAndNil(FArrResultPic[I][J]);
end;
if Assigned(FArrResultSPic[I]) then FreeAndNil(FArrResultSPic[I]);
if Assigned(FLabResultNum[I]) then FreeAndNil(FLabResultNum[I]);
if Assigned(FLabResultRate[I]) then FreeAndNil(FLabResultRate[I]);
end;

end;
jjbhql 2009-07-03
  • 打赏
  • 举报
回复
这是所有的Create和Destory,TPNGObject和 TPictureButtOn是第三方的并且已经用过多次肯定是没有问题的。
type
ResultType = (Idle, Ping, Land);
ResultTypeOption = (clNone,clLandPair,clIdlePair,clLandIdle);
CResultOption =packed record
arResultType: TBetPlace;
IsLandPair: Boolean;
IsIdlePair: Boolean;
end;


TableArrPos =packed record
X: Integer;
Y: Integer;
end;


constructor TBJLResultTable.Create(AOwer: TComponent);
var
I: ResultType;
J:ResultTypeOption;
PicPng: TPNGObject;
function CreateLable(LPoint: TPoint; LColor: TColor; LCaption: string):TLabel;
begin
Result :=TLabel.Create(Self);
Result.Font.Color := LColor;
Result.Font.Name :='宋体';
Result.Font.Height := -15;
Result.Font.Style :=[fsBold];
Result.Transparent :=True;
Result.Parent :=Self;
Result.Visible :=True;
Result.Left :=LPoint.X;
Result.Top :=LPoint.Y;
Result.Width :=40;
Result.Caption :=LCaption;
end ;
begin
inherited;
EmptyTable;

PicPng :=TPNGObject.Create;
PicPng.LoadFromResourceName(HInstance, 'TbaleBack');
FBackGround :=TPicture.Create;
FBackGround.Graphic :=PicPng;
Width :=FBackGround.Width;
Height :=FBackGround.Height;

for I :=Low(ResultType) to High(ResultType) do
begin
for J := Low(ResultTypeOption) to High(ResultTypeOption) do
begin
PicPng.LoadFromResourceName(HInstance, Format('%s%d',[FarrResName[I],ord(J)]));
FArrResultPic[I][J] := TPicture.Create;
FArrResultPic[I][J].Graphic :=PicPng;
end;

PicPng.LoadFromResourceName(HInstance,Format('%s',[FArrSResName[I]]));
FArrResultSPic[I] :=TPicture.Create;
FArrResultSPic[I].Graphic :=PicPng;

FLabResultNum[I] := CreateLable( Point(72+ord(I)*153, 42), FArrColor[I],'0');
FlabResultRate[I] := CreateLable( Point(102+ord(I)*153, 42), clBlack,'00.00%');
end;

FLabLandPair := CreateLable(Point(196,64),clBlack,'0');
FLabIdlePair := CreateLable(Point(75, 64),clBlack,'0');
FLabLandKing := CreateLable(Point(317,64),clBlack,'0');
FLabIdleKing := CreateLable(Point(438,64),clBlack,'0');

FCloseBtn :=TPictureButton.Create(Self);
FCloseBtn.Parent :=Self;
FCloseBtn.Visible := True;
FCloseBtn.Left :=475;
FCloseBtn.Top :=10;
FCloseBtn.OnClick := CloseBtnClick;

try
PicPng.LoadFromResourceName(Hinstance, 'CloseComm');
FCloseBtn.CommPicture.Graphic := PicPng;
PicPng.LoadFromResourceName(HInstance, 'CloseMove');
FCloseBtn.MovePicture.Graphic :=PicPng;
PicPng.LoadFromResourceName(HInstance,'CloseDown');
FCloseBtn.DownPicture.Graphic :=PicPng;
finally
if Assigned(PicPng) then
FreeAndNil(PicPng);
end;

end;


destructor TBJLResultTable.Destroy;
var
I:ResultType;
J:ResultTypeOption;
begin
inherited;

if Assigned(FLabLandPair) then FreeAndNil(FLabLandPair);
if Assigned(FLabIdlePair) then FreeAndNil(FLabIdlePair);
if Assigned(FLabLandKing) then FreeAndNil(FLabLandKing);
if Assigned(FLabIdleKing) then FreeAndNil(FLabIdleKing);
if Assigned(FCloseBtn) then FreeAndNil(FCloseBtn);

for I := Low(ResultType)to High(ResultType) do
begin
for J := Low(ResultTypeOption) to High(ResultTypeOption) do
begin
if Assigned(FArrResultPic[I][J]) then FreeAndNil(FArrResultPic[I][J]);
end;
if Assigned(FArrResultSPic[I]) then FreeAndNil(FArrResultSPic[I]);
if Assigned(FLabResultNum[I]) then FreeAndNil(FLabResultNum[I]);
if Assigned(FLabResultRate[I]) then FreeAndNil(FLabResultRate[I]);
end;

end;
jjbhql 2009-07-03
  • 打赏
  • 举报
回复
就写在这个控件的Destroy里拉,要不我把整个单元的代码贴出来?只是里面用了几个第三方控件,可能会造成不好理解。
beifangke 2009-07-03
  • 打赏
  • 举报
回复
释放写在哪里的?

829

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧