Living management management using DEPHI components

xiaoxiao2021-03-06  103

It is well known that the objects in the creation design mode create an object.

But where is the object released?

Of course, for abstract factories and factory methods and prototype modes, you can save a reference and release it off in need. There is no such reference in Singleton mode, or cannot be sure that it is created.

At this time, the life management mechanism of DEPHI components is very useful. You only need to inherit from Tcomplace and set your own Owner to another managed component in the constructor (generally your form, frame. The like), the object you created will be automatically released when its Owner is released. Oh, no need to envy the Java programmer :)

When is it inherited from Tcompenent?

My suggestion is: As long as you can.

Since it is not predictable when creating the class (I can't, maybe you can), it is best to inherit the new class from Tcompenent or Tcompenent, which prevents the life of the object from being treated, and Tcompenent Implement an Iinterface interface, because all of our interfaces are inherited from Iinterface (IUnknow is also), so if your class wants to implement the interface, it is best to inherit from Tcompenent or Tcompenent, otherwise you must implement it yourself. The following three methods are below.

??? function queryinterface (const iid: tguid; out obj): hResult; stdcall;

??? function _addref: integer; stdcall;

??? function _release: integer; stdcall;

Of course, if you have to inherit the base class is not inherited from Tcompenent, then I have no good way.

When will I release it to Owner release?

Let's talk about the difference, you can release resources more efficiently, there are fewer resources. Released by Owner, you can reduce the error rate, reduce the code write amount, make the code more concise.

So, if you need to create an unknown number of creation, you will be released manually, only one or limited time and less affected resource occupation is good. A simple principle, all objects released in DETROY can be released by Owner. The temporary variable created in the method is absolutely unable to set it, because although the last object is still released, you may create the same object multiple times, causing a lot of resource waste, unless you implement it into Singleton.

转载请注明原文地址:https://www.9cbs.com/read-125184.html

New Post(0)