I have a project ported to Delphi 11.2 from 10.2 which appears to be going very well. So far.
I now have an issue with a tried and tested function that checks to see if a file share exists. I have put this into its own test form to make it stand-alone.
The issue with this is the NetApiBufferFree(retBuf). This causes a 0x0000374 (Heap corruption) and the program bombs (no error), sometimes. The pattern of failures is very frustrating.
The same files with Delphi 10.2 work fine, so I’m not sure if it is the code or the 11.2 compiler, or maybe a definition somewhere outside the unit.
Here is the test form used:
TForm47 = class(TForm)
procedure FormShow(Sender: TObject);
TNetShareGetInfo = Function(servername : LPWSTR; netname : LPWSTR;
level : DWORD; var bufptr : LPBYTE) : DWORD stdcall;
TNetApiBufferFree = function(var bufptr: LPBYTE): DWORD stdcall;
function GetPCName: string;
u: array [0 .. 255] of Char;
sz := 256 + 1;
Result := u;
procedure shareExists(srvName, shareName: String; var shrExists, err: boolean);
reqdSrv, reqdShare: array[0..255] of WideChar;
Handle := LoadLibrary('netapi32.dll');
if Handle <> 0 then
shrExists := false;
err := False;
StringToWideChar(srvName, reqdSrv, Length(srvName) + 1);
StringToWideChar(shareName, reqdShare, Length(shareName) + 1);
@NetShareGetInfo := GetProcAddress(Handle, 'NetShareGetInfo');
if @NetShareGetInfo <> nil then
retVal := NetShareGetInfo(reqdSrv, reqdShare, 2, retBuf);
if (retVal = 0) then
@NetApiBufferFree := GetProcAddress(Handle, 'NetApiBufferFree');
shrExists := true
procedure TForm47.FormShow(Sender: TObject);
shareExists('\\' + GetPCName, 'LGCLIENT$', ShareExist, Err);
The program bombs when the share already exists. Only in D11, and inconsistently, I can run it multiple times without a problem, then suddenly it starts crashing.
Any ideas, or any other known way to do this that doesn’t involve a third party?
Thanks for all the replies.
Thinking about it last night, and thinking about what has changed in D11, I thought of ASLR. Turning this off sorts the problem out. Very odd. With ASLR off I have run the program many times without an issue. As soon as I turn it back on, it fails repeatedly.
Perhaps D11’s implementation of ASLR isn’t so good…
I’m guessing you’re right.
Got rid of the StringToWideChar, changed retVar to a pointer, changed reqdSrv, reqdShare to PWideChars.
The test program still randomly fails. Until… I removed the var from NetApiBufferFree. Now it appears to be stable. I would never have thought to look there. Thank you, you’ve saved me many more hours of frustration.