多线程使用过程中出现内存泄露,大家来帮我查查在哪个地方。分不多,谢谢帮忙啦。

qtc26 2012-03-09 02:14:45
症状:内存没有明显的上升,但软件挂一天基本就是10几万的句柄数了。
功能说明:不停创建新线程去访问各种不同的网页服务器获得数据显示在窗体listbox中。


相关代码:

unit uhdthread;

interface

uses
Classes,gongyhanshu,SysUtils,EncdDecd,HTTPApp,md5,strutils,math,md5qqpassword,dateutils,ugrthread,forms;


type
thdthread = class(TThread)
private
fhdqq:sendrecord;//自定义记录类型,访问网站需要数据
fhdid:string;;//网页名称,
procedure run;
procedure yilido;
procedure piaowudo;//有很多类似的函数
......
{ Private declarations }
protected
procedure Execute; override;
public
constructor create(huodongqq:sendrecord;hdid:string);
end;

implementation
uses unit1;//主窗体单元文件
var
recivedata:reciverecord;//自定义记类类型,接受网站返回数据

constructor thdthread.create(huodongqq:sendrecord;hdid:string);
begin
inherited create(false);
fhdqq:=huodongqq;
fhdid:=hdid;
end;

procedure thdthread.run;
begin
if fhdid='伊利' then yilido;
if fhdid='QQ票务' then piaowudo;
.....//很多这样的条件语句
end;

procedure thdthread.piaowudo;
var
i:integer;
begin
if fhdqq.hdid='勇士自动' then //自动访问----通过嵌套调用此线程对象完成
begin
for i:=form1.ListView1.Selected.index to form1.ListView1.Items.Count-1 do
begin
fhdqq.hdid:='勇士集结';
thdthread.create(fhdqq,'QQ票务');
sleep(strtoint(form1.pwframe1.Edit2.Text));
end;
end;
if fhdqq.hdid='勇士集结' then //手动访问
begin
......//fhdqq添加访问必需数据
recivedata:=getorpost(fhdqq,false);//接受返回数据
......//处理返回数据并显示
end;
end;

................//很多类似函数

procedure thdthread.Execute;
begin
try
{self.Synchronize(run); }//这里没有采取同步措施
run;
except
listinsert(fhdqq.qqnum,fhdid,'线程运行出错');//句柄数增加大概到15万多的时候只是偶尔会出现这种现象。
end; //这是没有try..finally..end模块也没有设置FreeOnTerminate:true,(好像DELPHI7默认是true)
end;

---------------附公用函数function getorpost(var huodongqq:sendrecord;ckenable:boolean):reciverecord;
var
gethttp:tidhttp;
response,postd:tstringstream;
tmpck:string;
begin
......//idhttp基本设置
response:=tstringstream.Create('');
//开始发送
try
try
if huodongqq.postdata='' then gethttp.Get(huodongqq.url,response)
else
begin
postd:=tstringstream.Create('');
try
postd.WriteString(huodongqq.postdata);
gethttp.Request.ContentLength:=postd.Size;
gethttp.post(huodongqq.url,postd,response);
finally
postd.Free;
end;
end;
if gethttp.responsecode<>200 then
begin
result.cookie:='';
result.response:='402orother';
exit;
end;
if huodongqq.acceptencoding='gzip,deflate' then DecompressGZip(response,response);
tmpck:='';
if ckenable =true then tmpck:=cookieread(gethttp.Response.RawHeaders);
result.cookie:=tmpck;
result.response:=response.datastring;
except
result.cookie:='';
result.response:='网络繁忙';
end;
finally
gethttp.Free;
response.Free;
end;
end;
...全文
192 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-05-11
  • 打赏
  • 举报
回复
这线程里面递归创建线程。这个代码太操蛋了。
沙滩脚印 2012-05-11
  • 打赏
  • 举报
回复
是不是什么东西在前面free了,到后面free不上了
头些天写个程序,就是那样,在前面遇到出错不小心free了,运行到后面就出错
qtc26 2012-03-10
  • 打赏
  • 举报
回复
我是担心同步的话一个个的等会影响线程向LISTBOX写入数据的速度,毕竟多的时候一秒中要写入几十次,上百次。没办法明天同步后看看会不会增加了,谢谢。。
Mit1208 2012-03-09
  • 打赏
  • 举报
回复
线程中操作VCL的同时,如果不给VCL控件同步的话,正在这时,主线程也操作VCL的话,那启不是同时访问或修改了控件数据了?肯定会出错的。
Mit1208 2012-03-09
  • 打赏
  • 举报
回复
在线程中操作VCL控件就会出现泄漏
将它使用syncrhonize同步后再操作。
即使你的程序只有一个线程在操作,也最好使用线程同步。
qtc26 2012-03-09
  • 打赏
  • 举报
回复
偶的神啊。。我这是辅助程序。
我一会想去农场偷菜,一会想去魔法卡片练卡片,一会想去发两个微博。而且是成千上万个QQ号,一会想去刷分投票,不拼命创建线程怎么完得成任务啊。。。也得在主窗体中显示完成得怎么样啊。。

而我关心的是内存泄露问题啊。。
我看见佛 2012-03-09
  • 打赏
  • 举报
回复
用Timer试试。
mhhaifeng 2012-03-09
  • 打赏
  • 举报
回复
不好意思 打错了
为什么要不停新线程? 获取的数据都在listbox显示?

我觉得 建一个线程就可以了

http://topic.csdn.net/u/20110217/11/56577c65-2e77-4f8f-b541-0be47cdb4d60.html?58725
mhhaifeng 2012-03-09
  • 打赏
  • 举报
回复
不停创建新线程去访问各种不同的网页服务器获得数据显示在窗体listbox中
不明白 为什么要不建新线程? 获取的数据都在listbox显示?
明显也只能一个个来 要不然会乱套
一个个取不行么?效率差多少了?
qtc26 2012-03-09
  • 打赏
  • 举报
回复
如有好的建议也欢迎提出,加分也可以。。。

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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