Windows版Firemonkey下的Delphi ARC [英] Delphi ARC under Firemonkey for Windows

查看:133
本文介绍了Windows版Firemonkey下的Delphi ARC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看这段视频,其中Marco正在谈论自动参考计数.我已经知道在Android和iOS(Firemonkey)下我的对象被引用计数,因此我不需要try finally块.

I am watching this video where Marco is talking about Automatic Reference Counting. I already knew that under Android and iOS (Firemonkey) my objects are ref counted so I don't need the try finally block.

引用计数实现是与平台(VLC或FMX)还是与操作系统一起工作的?

Does the reference count implementation work according with the platform (VLC or FMX) or with the OS?

我的意思是:

var a: TObject;
begin
 a := TObject.Create;
 a.use1;
 a.use2;
end;

如果Firemonkey在Android/iOS/Mac上运行,则对Firemonkey很有好处,我没有内存泄漏.但是,如果我在Windows(并且使用过Firemonkey)下运行此程序,由于没有引用计数,我是否还会发生内存泄漏?

This is good in Firemonkey if it runs on Android/iOS/Mac, I have no memory leak. But if I ran this under Windows (and I have used Firemonkey) do I still have the memory leak due to no ref count?

无论如何,从视频中我都知道,即​​使在ARC下,调用Freetry finally也不错,但没用.

Anyway from the video I have understood that a try finally with a call of Free, even under ARC, is not bad usage but it's just useless.

推荐答案

ARC是按OS平台而不是GUI框架实现的.

ARC is implemented per OS platform, not GUI framework.

Android,iOS和Linux编译器对对象使用ARC内存管理. Windows和OSX编译器使用经典的手动内存管理,其中仅在接口引用而不是对象上支持ARC.

The Android, iOS and Linux compilers use ARC memory management for objects. The Windows and OSX compilers use the classic manual memory management, where ARC is supported only on interface references, not objects.

VCL是仅Windows的框架,仅在经典编译器下运行.

VCL is a Windows-only framework, it runs only under classic compilers.

另一方面,FMX作为跨平台框架,并根据运行的OS平台使用不同的内存管理系统.

On the other hand, FMX as a cross-platform framework, and uses different memory management systems, depending on which OS platform it runs on.

try...finally Free块在ARC编译器上确实是无用的(在与Free方法结合使用的安全保护对象发布的上下文中). 但是,如果编写必须在两个内存管理系统下都可以使用的跨平台代码,则必须使用try...finallyFree.

try...finally Free blocks are indeed useless on ARC compilers (in context of safe-guarding object release in combination with Free method). However, if you write cross-platform code that must work under both memory management systems, you must use try...finally and Free.

另一方面,如果编写仅在ARC下必须运行的代码,则可以安全地省略try...finallyFree.

On the other hand, if you write code that only must work under ARC, you can safely omit try...finally and Free.

但是,在ARC上,您可能需要使用Free(在ARC编译器上,它转换为nil分配),或者如果需要在某个点之前释放对象,则直接将nil分配给对象/接口引用.引用将超出范围.

However, on ARC you may want to use Free (on ARC compiler it translates to nil assignment) or directly assign nil to object/interface reference if you need to release object at certain point, before it's reference would go out of scope.

上述规则有一个重要的例外-TComponent后代(包括Firemonkey GUI组件和控件),如果要在代码中创建和释放这些实例,则必须将try...finally Free块替换为try...finally DisposeOf.

There is one important exception to the above rules - TComponent descendants (that includes Firemonkey GUI components and controls) where try...finally Free blocks have to be replaced with try...finally DisposeOf if you are creating and releasing those instances in code.

您可以在如何在Android/iOS中释放组件的更多信息

此处要注意的重要事项:DisposeOf具有非常特定的用途,它不是打破参考周期并在ARC下释放对象的通用解决方案.在任何地方盲目使用它可能会导致内存泄漏.可以在上述问题/解答中的 DisposeOf的陷阱

Important thing to note here: DisposeOf has very specific purpose, it is not universal solution for breaking reference cycles and releasing objects under ARC. Using it blindly in all places can result in memory leaks. More elaborate answer can be found in above Q/A under Pitfalls of DisposeOf

这篇关于Windows版Firemonkey下的Delphi ARC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆