我写了一个Dll,在应用程序中调用没有发现什么问题
但是我在调试的时候却出现如下情况:(我的Dll中有窗体)我在每次关闭主调用程序时都会弹出CPU View框,我必须在Delphi IDE环境中手动关闭,才可以,这种情况的出现我估计是因为我资源销毁的问题导致了内存泄漏。不知是否这样?有什么办法能解决呢?或者说用什么方法能检测出是什么地方导致了内存泄漏呢?有工具可以检测吗?

解决方案 »

  1.   

    你是不是在DLL中用的无模式窗体,并且在DLL中没有引出窗体释放的函数
      

  2.   

    我的代码如下,有什么问题吗?谢谢function SearchLib(): Integer;stdcall;
    var
      frmSearch: TfSearch;
      theDb: TmyDM;
    begin
      theDb := TMyDM.Create(nil);
      if not theDb.OpenDb(0) then
      begin
        Result := -1;       //操作失败,数据库打开失败
        Exit;
      end;
      try
        frmSearch := TfSearch.Create(theDb);
        frmSearch.ShowModal;
        Result := theDb.resRows;
        frmSearch.Free;
      except
        Result := -1;
      end;
      theDb.Free;
    end;
      

  3.   

    在调用DLL的时候把当前的句柄一起传到DLL里去,并把DLL的Application.Handle设置为当前句柄。(书上是这么说的)。
      

  4.   

    所有Free都放在try……finally……end的finally end 子句中。
    并尽可能修正为:if assigned(x) then begin
      x.Free;
      x := nil;
    end;(注:ShowModal 对Handle没有特殊要求)
      

  5.   

    stella53(慕蓉云风) :我照你说的做了,怎么还是要弹出Cpu View呢?
    代码如下,请指出问题在什么地方?function SearchAll(H: THandle;intFunc: Integer;var intOperType: Integer;var strKeyWords: PChar): Integer;stdcall;
    var
      theDb: TmyDM;
      fRes: TfResult;
      fHit: TfrmSearching;
    begin
      Application.Handle := H;  theDb := TMyDM.Create(nil);
      if not theDb.OpenDb(0) then
      begin
        Result := -1;       //操作失败,数据库打开失败
        Exit;
      end;
      theDb.theFunc := intFunc;         //将调用的功能号存入DbLib单元  theDb.GetFuncInfo(theDb.theFunc);
      theDb.strSearchSql := theDb.GetSqlString;
      try
        fHit := TfrmSearching.Create(theDb);
        try
          fHit.ShowModal;
        finally
          fHit.Free;
        end;    ////////////////// 结束检索 /////////////////
        if theDb.resRows <= 0 then
        begin
          Application.MessageBox('对不起,没有检索到满足条件的记录!','提示',MB_OK + MB_ICONINFORMATION);
          Result := 0;
        end
        else
        begin
          fRes := TfResult.Create(theDb);
          try
            fRes.ShowModal;
            Result := theDb.resRows;
            intOperType := fRes.resOperType;          //获得操作类型
            strKeyWords := PChar(fRes.resKeyWords);   //获得关键字值
          finally
            fRes.Free;
          end;
        end;  finally
        theDb.Free;
      end;
    end;