我在应用程序中调用了多个DLL封装的窗体,在运行时如果只调用一个DLL程序,在退出时不会报错,但在调用2个以上的话就会报"access violation at 0x024932d6,read of address 0x02585c04"错误,我判断是内存泄露.下面是我的代码,请大家帮我看一下.
procedure TFrm_main.bbb1Click(Sender: TObject);
begin
 DllHandle1:= LoadLibrary('QuanInput.dll');
  if DLLHandle1<>0 then
  begin
    pproc := GetProcAddress(DllHandle1, 'InitDLL');
    if Assigned(pproc) then
    begin
      initDLL:=TinitDLL(pproc);
      InitDLL(DataModule1)
    end;
  end; Formname:='TFrm_quanint';
if DllHandle1 <> 0 then
  begin
    pproc := GetProcAddress(DllHandle1, 'GetFormClass');
    if Assigned(pproc) then
    begin
      GetFormClass := TGetFormClass(pproc);
      Form := GetFormClass(FormName,DataModule1).Create(nil);
      Form.ShowModal ;
      FreeAndNil(Form);;
    end;
  end;end;procedure TFrm_main.N6Click(Sender: TObject);
begin
 DllHandle2:= LoadLibrary('QuanInq.dll');
  if DLLHandle2<>0 then
  begin
    pproc := GetProcAddress(DllHandle2, 'InitDLL');
    if Assigned(pproc) then
    begin
      initDLL:=TinitDLL(pproc);
      InitDLL(DataModule1)
    end;
  end;
Formname:='TFrm_quaninq';
if DllHandle2 <> 0 then
  begin
    pproc := GetProcAddress(DllHandle2, 'GetFormClass');
    if Assigned(pproc) then
    begin
      GetFormClass := TGetFormClass(pproc);
      Form := GetFormClass(FormName,DataModule1).Create(nil);
      Form.ShowModal ;
      FreeAndNil(Form);;
    end;
  end;
end;procedure TFrm_main.FormDestroy(Sender: TObject);
begin
FreeLibrary(DLLHandle1);
FreeLibrary(DLLHandle2);
end;

解决方案 »

  1.   

    你点击一次按钮或者菜单就调用一次LoadLibrary当然不行了,把动态加载DLL的语句放在form.onCreate事件里面
      

  2.   

    我看你写的是动态加载dll,Form在dll文件中,可以用showmodal方式避免你的问题
      

  3.   

    把FreeLibrary(DLLHandle1); 放在TFrm_main.bbb1Click里,别放在 TFrm_main.FormDestroy(Sender: TObject); 里,FreeLibrary(DLLHandle2); 同样如此procedure TFrm_main.bbb1Click(Sender: TObject); 
    begin 
    DllHandle1:= LoadLibrary('QuanInput.dll'); 
      if DLLHandle1 <>0 then 
      begin 
        pproc := GetProcAddress(DllHandle1, 'InitDLL'); 
        if Assigned(pproc) then 
        begin 
          initDLL:=TinitDLL(pproc); 
          InitDLL(DataModule1) 
        end; 
      end; Formname:='TFrm_quanint'; 
    if DllHandle1 <> 0 then 
      begin 
        pproc := GetProcAddress(DllHandle1, 'GetFormClass'); 
        if Assigned(pproc) then 
        begin 
          GetFormClass := TGetFormClass(pproc); 
          Form := GetFormClass(FormName,DataModule1).Create(nil); 
          Form.ShowModal ; 
          FreeAndNil(Form);; 
        end; 
      end;   FreeLibrary(DLLHandle1);
    end; 
      

  4.   

    上面的方法都不行.syc-1981这位仁兄,我的程序里本来就是用的showmoodal.还请各位大侠多说说.
      

  5.   

    try
      loadlibrary('A.DLL')
    finally
      freeLibrary('A.DLL')
    end;
    另外,你这个重复代码太多。应该写成调用函数。
      

  6.   

    用showmodal不会存在dll窗体没关闭就退出程序,所以不会报错。
    如果是mdichild,不关闭dllform直接退出程序是会报错的,我得解决办法是主程序退出时向所有mdichild发送WM_CLOSE消息