因此,让我们来了解一下 dll。
如果要将字符串传递给 dll 调用,则必须使过程输入 PChar。否则你会得到数据curroption。
所以我们说我们的dll有
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 中,我们有一个 Form,其中有一个 FireDacConnection,但其中的任何组件或对象都可以完成工作。
问题是这个 PChar 被释放了两次并导致内存泄漏。我找不到在不导致内存泄漏的情况下通过 PChar 的方法。
你可以使用 fastmm,我使用 eurukalog,它写道
|+Leak #2: Type=UnicodeString: Ref count - 1, Content: "\r\n"; 总大小=18;计数=1 |
为什么 Unicode 字符串的 Ref 计数为 -1?如何预防?如何正确传递 Unicode 字符串?
我尝试过:将其作为 const 传递。复制它(如示例和 strpcopy 和 strcopy)使用局部变量来保存 PChar 的副本。
编辑:添加调用代码:
var
ConnectionName:WideString;
begin
ConnectionName := 'This Is My String';
LookPChar(PChar(ConnectionName));
end;
添加泄漏日志转储
|+Leak #2: Type=UnicodeString: Ref count - 1, Content: "\r\n"; 总大小=18;计数=1 | |------------------------------------------------- -------------------------------------------------- --------------------------------------------------| |00000002|04 |00000000|01D79D9C|outDll.dll|00009D9C|系统
| |_NewUnicodeString |23897[6] | |00000002|04 |00000000|008A11BC|myapp.exe |004A11BC|调用者
|TForm2 |Button4单击 |66[2] | |00000002|04 |00000000|00641C13|myapp.exe |00241C13|Vcl.Controls |TControl |单击 |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|System.Classes| |StdWndProc
|16600[8] | |00000002|03 |00000000|768162F7|user32.dll
|000162F7|USER32 | | (可能有间隙fnScSendMessage+815)| | |00000002|03
|00000000|76816D35|user32.dll |00016D35|USER32 |
| (可能 GetThreadDesktop+210)| | |00000002|03
|00000000|76816DE8|user32.dll |00016DE8|USER32 |
| (可能 GetThreadDesktop+389) | | |00000002|03
|00000000|76816E49|user32.dll |00016E49|USER32 |
| (可能 GetThreadDesktop+486)| | |00000002|03
|00000000|77420107|ntdll.dll |00010107|ntdll |
|KiUserCallbackDispatcher | | |00000002|03
|00000000|768196D0|user32.dll |000196D0|USER32 |
|发送信息W | | |00000002|03
|00000000|71AB459B|comctl32.dll |000A459B|comctl32 |
|加载图标度量 | | |00000002|03
|00000000|71AB45FE|comctl32.dll |000A45FE|comctl32 |
|加载图标度量 | | |00000002|03
|00000000|71AB4488|comctl32.dll |000A4488|comctl32 |
|加载图标度量 | | |00000002|03
|00000000|768162F7|user32.dll |000162F7|USER32 |
| (可能有间隙fnScSendMessage+815)| | |00000002|03
|00000000|76816D35|user32.dll |00016D35|USER32 |
| (可能 GetThreadDesktop+210)| | |00000002|03
|00000000|76820D32|user32.dll |00020D32|USER32 |
| (可能的 GetClientRect+192)| | |00000002|03
|00000000|76820D56|user32.dll |00020D56|USER32 |
|CallWindowProcW | | |00000002|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|System.Classes| |StdWndProc
|16600[8] | |00000002|03 |00000000|768162F7|user32.dll
|000162F7|USER32 | | (可能有间隙fnScSendMessage+815)| | |00000002|03
|00000000|76816D35|user32.dll |00016D35|USER32 |
| (可能 GetThreadDesktop+210)| | |00000002|03
|00000000|768177CE|user32.dll |000177CE|USER32 |
| (可能 CharPrevW+314) | | |00000002|03
|00000000|76817893|user32.dll |00017893|USER32 ||调度消息W | |
抱歉不清楚,我不知道如何在 stackoverflow 编辑器中保留标签。