我在应用程序中调用了多个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;
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;
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;
loadlibrary('A.DLL')
finally
freeLibrary('A.DLL')
end;
另外,你这个重复代码太多。应该写成调用函数。
如果是mdichild,不关闭dllform直接退出程序是会报错的,我得解决办法是主程序退出时向所有mdichild发送WM_CLOSE消息