function TShttp.getpipei(url:string;url2:string):k; var rex : TRegex; pipei:TMatch; pipei2:TMatchCollection; a,i,p,x:integer; idhttp1:Tidhttp; url_two:string; begin idhttp1:= Tidhttp.Create(nil); try code:=idhttp1.Get(url); rex:=TRegex.Create(zhengze); try pipei:=rex.Match(code); finally rex.Free; // 还有这个, 是不是应该在这里释放??? end; finally idhttp1.Free; // 是不是应该在这里释放??? end; end;
function TShttp.getpipei(url:string;url2:string):k; var rex : TRegex; pipei:TMatch; pipei2:TMatchCollection; a,i,p,x:integer; idhttp1:Tidhttp; url_two:string; begin try idhttp1:= Tidhttp.Create(nil); code:=idhttp1.Get(url);
var
rex : TRegex;
pipei:TMatch;
pipei2:TMatchCollection;
a,i,p,x:integer;
idhttp1:Tidhttp;
url_two:string;
begin
idhttp1:= Tidhttp.Create(nil);
try
code:=idhttp1.Get(url);
rex:=TRegex.Create(zhengze);
try
pipei:=rex.Match(code);
finally
rex.Free; // 还有这个, 是不是应该在这里释放???
end;
finally
idhttp1.Free; // 是不是应该在这里释放???
end;
end;
var
rex : TRegex;
pipei:TMatch;
pipei2:TMatchCollection;
a,i,p,x:integer;
idhttp1:Tidhttp;
url_two:string;
begin
try
idhttp1:= Tidhttp.Create(nil);
code:=idhttp1.Get(url);
rex:=TRegex.Create(zhengze);
pipei:=rex.Match(code);
finally
idhttp1.Free; // 是不是应该在这里释放???
rex.Free; // 还有这个, 是不是应该在这里释放???
end;
end;try finally 能不能这样写? 把要free的都放到 一个finally 中哦? 要不然要写很多 try finally 吧
如果create也在try..finally里头,万一create失败报异常,那free也会执行,那样就会free一个不存在的对象,就错上加错了。
比如 idhttp1 free 之后,该方法下面的代码又要使用idhttp1,这时 idhttp 需要重新 create 一次吗?
比如 idhttp1 free 之后,该方法下面的代码又要使用idhttp1,这时 idhttp 需要重新 create 一次吗?
可以重新 create。这种情况也可以把idhttp1声明成你的类的成员变量,在类的create和destroy中创建idhttp1和释放idhttp1,这样类的所有方法都用同一个idhttp。
record本身不需要释放,record里面的成员对象需要释放