nil owner在组件构造函数中的含义是什么 [英] What is the meaning of nil owner in component constructor

查看:179
本文介绍了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 ?

非常感谢:)


Thanks a lot :)

推荐答案

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

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

如果你在一个表单上删除一个组件,它的构造与窗体作为所有者。这意味着当表单是free'd时,它将释放它拥有的所有组件。如果您传递不同的所有者(例如,在运行时创建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天全站免登陆