memory-safety
Delphi memory and resource management patterns
Installation
Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.
Voir la source ↗GuidesInstructions
Memory and Resource Management
Interface Reference Counting
TInterfacedObject provides automatic reference counting. When the last interface reference goes out of scope, the object is freed automatically. Always assign to interface-typed variables to activate reference counting.
Object Ownership
If you call Create, you must call Free (unless the object is managed via interfaces). No exceptions.
try/finally Pattern
All resource allocations must be wrapped in try/finally:
LStream := TFileStream.Create(APath, fmOpenRead);
try
// use LStream
finally
LStream.Free;
end;
Cache Management
Router4D uses MAX_FRAME_COUNT = 25 to limit the number of cached frames. When the cache exceeds this limit, the oldest entries are removed to prevent unbounded memory growth.
Common Pitfalls
-
Mixing interface and object references: Holding both an interface reference and an object reference to the same
TInterfacedObjectinstance causes premature or double frees. Pick one. -
Forgetting to nil references: After freeing an object, set the variable to
nilto avoid dangling pointer access. - Circular interface references: Two objects holding interface references to each other will never be freed. Break cycles by using weak references or explicit cleanup.
FreeAndNil in Destructors
Use FreeAndNil for owned objects in destructors to safely release and nil fields:
destructor TRouter4DHistory.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
Exemples
Consultez les exemples du dépôt source.
Voir la source ↗Historique source
- Date d’indexation
- 2026-03-11
- Mise à jour de l’index
- 2026-03-11
- Branch
- main
Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!