dll中的内存泄露问题

iqyely 2009-08-23 12:11:46
在运行程序中出现了内存泄露,请大家帮忙看下该怎么解决呢?

dll中的代码:

uses Registry,Classes;

procedure RegReadAllKey(RootPath:Cardinal;OpenPath:pChar;YN:boolean;const List:TStringList);

implementation

procedure RegReadAllKey(RootPath:Cardinal;OpenPath:pChar;YN:boolean;const List:TStringList);
var Reg:TRegistry;
begin
Reg:=TRegistry.Create;
try
with Reg do
begin
RootKey:=RootPath;
if OpenKey(OpenPath,YN) then
GetKeyNames(List);
end;
finally
Reg.CloseKey;
Reg.Free;
end;
end;


主程序代码:

var
Form1: TForm1;
Th:Thandle;

type
TRegReadAllKey=procedure(RootPath:Cardinal;OpenPath:pChar;YN:boolean;const List:TStringList);

implementation

{$R *.dfm}

procedure ReadRegAllKey(RootPath:Cardinal;OpenPath:string;YN:boolean;const List:TStringList);
var Func:TRegReadAllKey;
begin
if Th>0 then
try
begin
@Func:=GetProcAddress(Th,PChar('RegReadAllKey'));
if @Func<>nil then
Func(RootPath,pChar(OpenPath),YN,List);
end;
finally
//FreeLibrary(Th);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var TempList:TStringList;
i:integer;
begin
TempList:=TStringList.Create;
ReadRegAllKey(HKEY_LOCAL_MACHINE,'SOFTWARE',false,TempList);//为什么调用dll后会出现内存泄漏呢?
for I := 0 to TempList.Count - 1 do
memo1.Lines.Add(TempList[i]);
TempList.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Th:=LoadLibrary('test.dll');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeLibrary(Th);
end;



在dll中加入了CheckMem.pas文件,程序运行后注册表中的子键有几个就出现几处内存泄露,这个该怎么解决下呢?
...全文
319 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
iqyely 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liangpei2008 的回复:]
感觉应该是dll中 GetKeyNames(List)引起的!
从主调传递一个对象至DLL中,本来没有问题的,但GetKeyNames(TStrings)参数会对TStringList进行向上转型,造成主调与DLL模块中TStringList的RTTI地址不一致所致!
可以用共享内存的方法解决,在双方的.dpr单元的uses列表的第一项添写FastMM4或ShareMem应该可以解决

[/Quote]

将TStringList修改为TStrings后却报出abstract error错误,这是什么原因啊?
iqyely 2009-09-13
  • 打赏
  • 举报
回复
继续寻求解决办法。
iqyely 2009-09-10
  • 打赏
  • 举报
回复
把List:TStringList作为返回值后怎么接收呢?我这边老是出错啊,有没有类似的代码呢?
ahjoe 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 iqyely 的回复:]

GetKeyNames(List)不是已经对list赋值了么?
[/Quote]

没有。List是个对象,这里只是对List的成员赋值。
iqyely 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ahjoe 的回复:]
引用 5 楼 iqyely 的回复:
引用 4 楼 ahjoe 的回复:
引用 3 楼 ljmanage 的回复:
const List:TStringList
能返回出来吗?


能的


把它作为返回值?


函数中并没有对参数 List 进行赋值。
[/Quote]

GetKeyNames(List)不是已经对list赋值了么?
liangpei2008 2009-08-31
  • 打赏
  • 举报
回复

感觉应该是dll中 GetKeyNames(List)引起的!
从主调传递一个对象至DLL中,本来没有问题的,但GetKeyNames(TStrings)参数会对TStringList进行向上转型,造成主调与DLL模块中TStringList的RTTI地址不一致所致!
可以用共享内存的方法解决,在双方的.dpr单元的uses列表的第一项添写FastMM4或ShareMem应该可以解决
ahjoe 2009-08-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 iqyely 的回复:]
引用 4 楼 ahjoe 的回复:
引用 3 楼 ljmanage 的回复:
const List:TStringList
能返回出来吗?


能的


把它作为返回值?
[/Quote]

函数中并没有对参数 List 进行赋值。
iqyely 2009-08-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ahjoe 的回复:]
引用 3 楼 ljmanage 的回复:
const List:TStringList
能返回出来吗?


能的
[/Quote]

把它作为返回值?
ahjoe 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ljmanage 的回复:]
const List:TStringList
能返回出来吗?
[/Quote]

能的
李_军 2009-08-24
  • 打赏
  • 举报
回复
const List:TStringList
能返回出来吗?
ahjoe 2009-08-23
  • 打赏
  • 举报
回复
调用dll后,TempList有增加几条内容, 不觉得有什么内存泄漏。
贝隆 2009-08-23
  • 打赏
  • 举报
回复
帮顶

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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