由pchar引起的delphi调用delphi dll内存泄漏
让我们来看看dll 如果要将字符串传递给dll调用,则必须使过程输入PChar。否则,您将获得数据更新选项 所以我们说我们的dll由pchar引起的delphi调用delphi dll内存泄漏,delphi,memory-leaks,delphi-xe7,pchar,Delphi,Memory Leaks,Delphi Xe7,Pchar,让我们来看看dll 如果要将字符串传递给dll调用,则必须使过程输入PChar。否则,您将获得数据更新选项 所以我们说我们的dll procedure LookPchar(pfff:Pchar);stdCall;External 'OutDll.dll'; 这很好。现在让我们看看我们在dll dpr中声明的内容: procedure LookPchar(pfff:Pchar); begin with TForm1.Create(nil) do try s
procedure LookPchar(pfff:Pchar);stdCall;External 'OutDll.dll';
这很好。现在让我们看看我们在dll dpr中声明的内容:
procedure LookPchar(pfff:Pchar);
begin
with TForm1.Create(nil) do
try
show;
FireDacConnection.ConnectionName := (Copy(pfff,1,100));
finally
free;
end;
end;
exports LookPchar;
嗯,在Dll中我们有一个表单,其中有一个FireDacConnection,但是其中的任何组件或对象都可以完成这项工作
问题是此PChar被释放两次,并导致内存泄漏。我无法找到一种无原因通过PChar的方法来避免内存泄漏
你可以用fastmm,我用eurukalog,它写
|+泄漏#2:Type=UnicodeString:Ref count-1,内容:“\r\n”;全部的
尺寸=18;计数=1|
为什么Unicode字符串的引用计数为-1?如何预防?如何正确传递Unicode字符串
我尝试的是:
作为常量传递它。
复制它(如示例中所示,并使用strcopy和strcopy)
使用局部变量保存PChar的副本
编辑:
添加调用代码:
var
ConnectionName:WideString;
begin
ConnectionName := 'This Is My String';
LookPChar(PChar(ConnectionName));
end;
添加泄漏日志转储
|+泄漏#2:Type=UnicodeString:Ref count-1,内容:“\r\n”;总尺寸=18;计数=1|
|-----------------------------------------------------------------------------------------------------------------------------------------|| 00000002 | 04 | 00000000 | 01D79D9C | outDll.dll | 00009D9C |系统|| | | u23897[6]| |00000002 | 04 | 00000000 | 008A11BC | myapp.exe | 004A11BC |调用方
|t按M2 |按钮4点击| 66[2]| |00000002 | 04 | 00000000 | 00641C13 | myapp.exe | 00241C13 | Vcl.Controls |t控制|单击| 7348[9]| |00000002 | 04 | 00000000 | 00646172 | myapp.exe | 00246172 | Vcl.Controls |TWinControl | WndProc | 10038[153]| |00000002 | 04 | 00000000 | 0065B71C | myapp.exe | 0025B71C | Vcl.StdCtrls |TButtonControl | WndProc | 5163[13]| |00000002 | 04 | 00000000 | 006462D7 | myapp.exe | 002462D7 | Vcl.Controls || DoControlMsg | 10107[12]| |00000002 | 04 | 00000000 | 00646172 | myapp.exe | 00246172 | Vcl.Controls |TWinControl | WndProc | 10038[153]| |00000002 | 04 | 00000000 | 0070B240 | myapp.exe | 0030B240 | Vcl.Forms
|TCustomForm | WndProc | 4427[206]| |00000002 | 04 | 00000000 | 006457AC | myapp.exe | 002457AC | Vcl.Controls |TWinControl | MainWndProc | 9750[3]| |00000002 | 04 | 00000000 | 004F7614 | myapp.exe |000F7614 |系统.类| | StdWndProc
|16600[8]| | 00000002 | 03 | 00000000 | 768162F7 | user32.dll
|000162F7 |用户32 | |(可能 gapfnScSendMessage+815)| | | 00000002 | 03
|00000000 | 76816D35 | user32.dll | 00016D35 | user32 |
|(可能是桌面+210)| | | 00000002 | 03
|00000000 | 76816DE8 | user32.dll | 00016DE8 | user32 |
|(可能是桌面+389)| | | 00000002 | 03
|00000000 | 76816E49 | user32.dll | 00016E49 | user32 |
|(可能是桌面+486)| | | 00000002 | 03
|00000000 | 77420107 | ntdll.dll | 0000107 | ntdll |
|KiUserCallbackDispatcher | | | 00000002 | 03
|00000000 | 768196D0 | user32.dll | 000196D0 | user32 |
|SendMessageW | | | 00000002 | 03
|00000000 | 71AB459B | comctl32.dll | 000A459B | comctl32 |
|负荷计量| | | 0000000 2 | 03
|00000000 | 71AB45FE | comctl32.dll | 000A45FE | comctl32 |
|负荷计量| | | 0000000 2 | 03
|00000000 | 71AB4488 | comctl32.dll | 000A4488 | comctl32 |
|负荷计量| | | 0000000 2 | 03
|00000000 | 768162F7 | user32.dll | 000162F7 | user32 |
|(可能的gapfnScSendMessage+815)| | | 00000002 | 03
|00000000 | 76816D35 | user32.dll | 00016D35 | user32 |
|(可能是桌面+210)| | | 00000002 | 03
|00000000 | 76820D32 | user32.dll | 00020D32 | user32 |
|(可能的GetClientRect+192)| | | 00000002 | 03
|00000000 | 76820D56 | user32.dll | 00020D56 | user32 |
|CallWindowProcW | | | 0000000 2 | 04
|00000000 | 00646282 | myapp.exe | 00246282 | Vcl.Controls | TWinControl
|DefaultHandler | 10079[30]| | 00000002 | 04
|00000000 | 00646172 | myapp.exe | 00246172 | Vcl.Controls | TWinControl
|WndProc | 10038[153]| | 00000002 | 04
|00000000 | 0065B71C | myapp.exe | 0025B71C | Vcl.StdCtrls |TButtonControl | WndProc | 5163[13]| |00000002 | 04 | 00000000 | 004F7614 | myapp.exe |000F7614 |系统.类| | StdWndProc
|16600[8]| | 00000002 | 03 | 00000000 | 768162F7 | user32.dll
|000162F7 |用户32 | |(可能 gapfnScSendMessage+815)| | | 00000002 | 03
|00000000 | 76816D35 | user32.dll | 00016D35 | user32 |
|(可能是桌面+210)| | | 00000002 | 03
|00000000 | 768177CE | user32.dll | 000177CE | user32 |
|(可能是CharPrevW+314)| | | 00000002 | 03
|00000000 | 76817893 | user32.dll | 00017893 | user32 | |调度消息w|| 抱歉,不清楚,我不知道如何在stackoverflow编辑器中保留选项卡
Copy(pfff,1100)
很奇怪。您可以直接使用pfff
,让编译器自动将指针转换为以null结尾的c
FireDacConnection.ConnectionName := pfff;
procedure LookPchar(pfff:Pchar); stdcall;