搜索

PythonReactSEOPDF
GitHub Skill

memory-safety

Delphi memory and resource management patterns

academiadocodigo☆ 119 仓库 Stars58 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

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 TInterfacedObject instance causes premature or double frees. Pick one.
  • Forgetting to nil references: After freeing an object, set the variable to nil to 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;

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!