QuickLib icon indicating copy to clipboard operation
QuickLib copied to clipboard

Memory leak in Quick.Commons.GetSpecialFolderPath

Open pmoitaly opened this issue 5 years ago • 2 comments

Using Delphi 10.3 in Windows 10, setting ReportMemoryLeaksOnShutdown to True detects a memory leak in GetSpecialFolderPath function. As stated in https://www.experts-exchange.com/questions/10206340/Locating-special-folder.html, maybe you could consider a solution like:

implementation uses Winapi.ActiveX; ... function GetSpecialFolderPath(folderID : Integer) : string; var malloc : IMalloc; ppidl: PItemIdList; begin if (SHGetMalloc(malloc)=NOERROR) then begin SHGetSpecialFolderLocation(0, folderID, ppidl); SetLength(Result, MAX_PATH); if not SHGetPathFromIDList(ppidl,{$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF}) then begin malloc.Free(ppidl); raise EShellError.create(Format('GetSpecialFolderPath: Invalid PIPL (%d)',[folderID])); end; SetLength(Result, lStrLen({$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF})); malloc.Free(ppidl); end; end;

In my test, that solved the problem.

Regards,

Paolo

pmoitaly avatar Apr 06 '20 09:04 pmoitaly

I tried with Windows 10 and Delphi 10.3:

ReportMemoryLeaksOnShutdown := True; for i := 0 to 100 do Writeln(path.USERPROFILE);

but no memory leaks was reported. Could you tell me how to reproduce it?

exilon avatar Jul 01 '20 20:07 exilon

I'm sorry, I no longer have the code I used for testing that method. I remember we used it to read APPDATA in a VCL application. I tested your code. With ReportMemoryLeaksOnShutdown you are right, no leak found. But a memory leak is still found by madExcept: here is a screenshot. PathMemoryLeak Using path.APPDATA: PathMemoryLeak_APPDA

When I used the solution I proposed above, no leak was found.

pmoitaly avatar Jul 02 '20 00:07 pmoitaly

Closing this issue as it has been inactive for an extended period of time and there has been no recent activity. If this issue is still relevant, please feel free to reopen it with updated information.

exilon avatar May 01 '23 17:05 exilon