首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

2013-03-19 
多线程使用过程中出现内存泄露,大家来帮我查查在哪个地方。分不多,谢谢帮忙啦。症状:内存没有明显的上升,但

多线程使用过程中出现内存泄露,大家来帮我查查在哪个地方。分不多,谢谢帮忙啦。
症状:内存没有明显的上升,但软件挂一天基本就是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;

[解决办法]
这线程里面递归创建线程。这个代码太操蛋了。

热点排行