| Description | Hierarchy | Fields | Methods | Properties |
type TImagesCache = class(TObject)
A cache of loaded images.
The idea is that instead of Image := LoadImage(...) call Image := Cache.LoadImage_IncReference(...). Later, instead of freeing this image, call LoadImage_DecReference(Image). From your point of view, things will work the same. But if you expect to load many images from the same FileName, then you will get a great speed and memory saving, because image will only be actually loaded once. Notes:
All passed here FileNames must be absolute, already expanded paths. In the future it's expected that this (just like LoadImage, actually) will be extended to load images from URLs.
Note that in case of problems with loading, LoadImage_IncReference may raise an exception, just like normal LoadImage. In this case it's guaranteed that no reference will be incremented, of course. If LoadImage_IncReference returns in a normal way, then it will return something non-Nil, just like LoadImage does.
For now, LoadImage is always called with AllowedImageClasses = [TRGBImage, TAlphaImage] and ForbiddenConvs = []. Why ? Because this is the use case that I need right now... (I'm going to use this for VRML texture nodes.) This will be fixed (LoadImage_IncReference will get AllowedImageClasses and ForbiddenConvs parameters, will compare and save them in TImageCache record) as soon as the need will arise.
LoadImage_DecReference alwas sets Image to Nil, like FreeAndNil.
Note that before destroying this object you must free all images, i.e. call LoadImage_DecReference for all images allocated by LoadImage_IncReference. This class is not a lousy way of avoiding memory leaks — it would be a bad idea, because it would cause sloppy programming, where memory is unnecessarily allocated for a long time. In fact, this class asserts in destructor that no images are in cache anymore, so if you compiled with assertions enabled, this class does the job of memory-leak detector.
![]() |
constructor Create; |
![]() |
destructor Destroy; override; |
![]() |
function LoadImage_IncReference(const FileName: string): TImage; |
![]() |
procedure LoadImage_DecReference(var Image: TImage); |
![]() |
constructor Create; |
![]() |
destructor Destroy; override; |
![]() |
function LoadImage_IncReference(const FileName: string): TImage; |
![]() |
procedure LoadImage_DecReference(var Image: TImage); |