组件构造函数中nil owner是什么意思 [英] What is the meaning of nil owner in component constructor

查看:25
本文介绍了组件构造函数中nil owner是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看这个问题 我现在想知道,组件构造函数中 nil 作为所有者是什么意思.

I was looking at this question and I'm wondering now, what is the meaning of nil as the owner in component constructor.

SomeComponent := TSomeComponent.Create(nil);

我知道,在使用这个构造函数时我应该自己释放它,但这是在创建时传递所有者的唯一原因吗?当我忘记释放它并关闭我的应用程序时会发生什么 - 这是否意味着该对象作为垃圾保留在内存中?

I know, that I should free it by myself when using this constructor, but is that the only reason to pass the owner at creation ? And what happens, when I forget to free it and close my application - does it mean that this object remains in memory as a garbage ?

非常感谢:)

推荐答案

这意味着你有责任自己释放它.

It means that you're responsible for freeing it yourself.

如果您将一个组件放在表单上,​​它会以表单作为所有者来构建.这意味着当表单被释放时,它将释放它拥有的所有组件.如果您传递不同的所有者(例如,在运行时创建 TButton 并使 TPanel 成为其所有者),则同样适用;当所有者被销毁时,它会释放其在此过程中拥有的所有组件.

If you drop a component on a form, it's constructed with the form as the owner. This means that when the form is free'd, it will free all of the components it owns. The same applies if you pass a different owner (for instance, creating a TButton at runtime and making a TPanel it's owner); when the owner is being destroyed, it frees all of the components it owns in the process.

使用 nil 作为所有者意味着您在代码中手动创建它,并且您将承担自行释放它的责任.如果您忘记了并且您的应用程序关闭,它的内存将被释放回操作系统.但是,如果您的应用程序在您忘记之后运行了很长时间,那么您就有一大块不应该使用的内存正在使用.

Using nil as the owner means that you're creating it manually in code, and you'll accept the responsibility to free it yourself. If you forget and your application closes, it's memory is released back to the operating system. However, if your application runs for a long time after you forget, you have a chunk of memory that's in use that shouldn't be.

这篇关于组件构造函数中nil owner是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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