[Lazarus] Safe temp file name

Rik van Kekem rik at graficalc.nl
Wed Nov 25 16:03:33 CET 2015

Aradeonas wrote:
 > I used GetTempFileName in a multi thread system and it causes problems
 > as you guess already.
I wasn't aware that GetTempFileName isn't threadsafe.
(Are you sure your code is correct?)

Graeme Geldenhuys wrote:
> On 2015-11-25 13:51, Aradeonas wrote:
>> So the best way in my mind is using GUID and maybe
>> use it in a loop for checking not existing for extra safety.
> So I think it is a pretty safe option to use as a temp file name, even
> in a multi-threaded environment.

Even Microsoft advises it in case you use GetTempFileName excessively.
 > Due to the algorithm used to generate file names, *GetTempFileName* 
can perform poorly when creating a large number of files with the same 
prefix. In such cases, it is recommended that you construct unique file 
names based on *GUID*s.
So it should be safe to use GUIDs for tempfilenames.

FPC trunk has this:
   Guid := TGUID.NewGuid.toString;

(I'm not sure if FPC 2.6.4 also some helpers for creating GUIDs)

Rik (rvk)

More information about the Lazarus mailing list