16,749
社区成员
发帖
与我相关
我的任务
分享
PRec=^TRec;
TRec=record
a:Integer;
b:Integer;
end;
procedure TForm1.RzButton2Click(Sender: TObject);
var
onex,oney:TRec;
List:TObjectlist;
strList:TStringList;
aone:TRec;
begin
{try
onex.a:=1;
onex.b:=2;
oney.a:=3;
oney.b:=4;
List:=TObjectList.Create;
List.Add(@onex) ;
List.Add(@oney);
List.Delete(0); //执行这句话报错,为什么
aone:=Prec(List[0])^;
ShowMessage(IntToStr(aone.a));
finally
List.Free;
end; }
try
onex.a:=1;
onex.b:=2;
oney.a:=3;
oney.b:=4;
strList:=TStringList.Create;
strList.AddObject('onex',@onex) ;
strList.AddObject('oney',@oney);
strList.Delete(0);
aone:=Prec(strList.Objects[0])^;
ShowMessage(IntToStr(aone.a));
finally
strList.Free;
end;
end;
function TStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(@SDuplicateString, 0);
end;
InsertItem(Result, S, AObject);
end;
procedure TStringList.InsertItem(Index: Integer; const S: string; AObject: TObject);
begin
Changing;
if FCount = FCapacity then Grow;
if Index < FCount then
System.Move(FList^[Index], FList^[Index + 1],
(FCount - Index) * SizeOf(TStringItem));
with FList^[Index] do
begin
Pointer(FString) := nil;
FObject := AObject;
FString := S;
end;
Inc(FCount);
Changed;
end;
PStringItem = ^TStringItem;
TStringItem = record
FString: string;
FObject: TObject;
end;
他创建了TStringItem对象用于关联String和你的对象,最终Delete的时候将会释放这个TStringItem对象,但是你自己添加的对象不会被释放。