Y
yxy2003yxy
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是代码运行时内存消耗越来越大,最后蓝屏,百思不得其解,问题在哪里?如何解决?急啊,请大侠赐教:
function TForm1.GetContent():string;
var
doc,indoc:IHTMLDocument2;
all,inall:IHTMLElementCollection;
len,i,inlen,k:integer;
item,initem:IHTMLElement;
conHtm,oldImgURL,newImgURL:string;
tmpStream:TFileStream;
myidhttp:TIdHTTP;
begin
conHtm:='';
if Assigned(WebBrowser2)then
begin
doc:=WebBrowser2.document as IHTMLDocument2;//据我测试,这个语句可能是出现内存泄漏,不确定
all:=doc.Get_all;
len:=all.Get_length;
for i:=0 to len-1 do //该循环找到正文元素
begin
item:=all.item(i,varempty) as IHTMLElement;
if or (item.getAttribute('id',0) = 'Content') then
begin
inall:= item.all as IHTMLElementCollection;
inlen:= inall.length ;
for k:=0 to inlen-1 do //该循环找到图片元素
begin
initem:=inall.item(k,varempty) as IHTMLElement;
if initem.Get_tagName = 'IMG'then
begin
intImage:=intImage+1;
oldImgURL:=initem.getAttribute('SRC',0);
intfilename:= intfilename+1;
strpicpath:=DateToStr(date)+IntToStr(intfilename)+'.jpg';
newImgURL:='newsimg/'+strpicpath;
initem.setAttribute('SRC',newImgURL,0);
tmpStream:=TFileStream.Create(newImgURL,fmCreate);
try
idhttp1.Get(oldImgURL,tmpStream); //把图片写到本地
except
on e:exception do
showMessage(e.message);
end;
tmpStream.Free;
end;
end;
conHtm:=item.innerHTML ;
break;
end;
end;
以下是某循环体中对函数的调用
begin
WebBrowser2.Navigate(item.href);
while WebBrowser2.ReadyState <READYSTATE_COMPLETE do
Application.ProcessMessages;
GetContent();
end;
function TForm1.GetContent():string;
var
doc,indoc:IHTMLDocument2;
all,inall:IHTMLElementCollection;
len,i,inlen,k:integer;
item,initem:IHTMLElement;
conHtm,oldImgURL,newImgURL:string;
tmpStream:TFileStream;
myidhttp:TIdHTTP;
begin
conHtm:='';
if Assigned(WebBrowser2)then
begin
doc:=WebBrowser2.document as IHTMLDocument2;//据我测试,这个语句可能是出现内存泄漏,不确定
all:=doc.Get_all;
len:=all.Get_length;
for i:=0 to len-1 do //该循环找到正文元素
begin
item:=all.item(i,varempty) as IHTMLElement;
if or (item.getAttribute('id',0) = 'Content') then
begin
inall:= item.all as IHTMLElementCollection;
inlen:= inall.length ;
for k:=0 to inlen-1 do //该循环找到图片元素
begin
initem:=inall.item(k,varempty) as IHTMLElement;
if initem.Get_tagName = 'IMG'then
begin
intImage:=intImage+1;
oldImgURL:=initem.getAttribute('SRC',0);
intfilename:= intfilename+1;
strpicpath:=DateToStr(date)+IntToStr(intfilename)+'.jpg';
newImgURL:='newsimg/'+strpicpath;
initem.setAttribute('SRC',newImgURL,0);
tmpStream:=TFileStream.Create(newImgURL,fmCreate);
try
idhttp1.Get(oldImgURL,tmpStream); //把图片写到本地
except
on e:exception do
showMessage(e.message);
end;
tmpStream.Free;
end;
end;
conHtm:=item.innerHTML ;
break;
end;
end;
以下是某循环体中对函数的调用
begin
WebBrowser2.Navigate(item.href);
while WebBrowser2.ReadyState <READYSTATE_COMPLETE do
Application.ProcessMessages;
GetContent();
end;