How can a class commit suicide?

Source: Internet
Author: User

In object-oriented programming, a class is an abstraction of things, and it is not a physical object. Therefore, this article describes how a Class Object achieves suicide; the reason for the improvement to the class level is that the function method for implementing suicide is provided by the class, and all objects of the class commit suicide by using this method. Why is class suicide required? When a class object ends its own life, it can be directly killed by the owner who created the object, for example, new), for example, detele! That's right. This is true in most cases, but there are always exceptions. Next, we will analyze when a class object will die! We call the expected Class Object A, and the other class object B. Scenario 1: A is A member variable of Class B. At this time, B is the master of A, and B will kill A before it dies. In this case, A becomes the burial object of B. Who makes B the master! Case 2: A is an object temporarily created in A function of B. After the function is completed, A's life ends naturally, and is actually killed by the system. At this time, A is just A small passthrough in this script. Case 3: A is created by B through the new method in A special time, and A gun always points to it. The gun aiming at it may be held by B, or transferred by B to C or D. As long as anyone points a gun to it, it must be obedient and follow the command.
At this time, when the end of A's life is completely dependent on the mood of the person holding the gun at it, as long as people are unhappy, they can be killed at any time through delete. Of course, if the person who points the gun to it forgets to kill it when they die, or if all the guns point to other places, it becomes a piece of leaked memory, no chance to die. Case 4: A is A global static object. At this time, its life and death are not left or right by others. It has a very long life. It is born with the start of the whole program and ends with the end of the whole program. Before the end of the entire program, it will be killed by the system with all static objects after other non-static classes or variables die. Case 5: there may be other dead ends for A. I will not list them here. Next, let's start with the topic to discuss the suicide behavior of the class. From the above situations, we can see that the death of object A is not voluntary, but is forced to kill it by the system or the host. So under what circumstances will A die? Scenario 1: Class A is A task executor. The task is characterized by the fact that after the task is executed, Class A must commit suicide or notify the superior to kill himself. For example, A is the video play performer class. When A superior creates A Class A object and notifies A of the necessary information about the resources to be played, A starts playing the video resource, since the playing end is only known at the bottom layer A, it is impossible for the upper level to know that the playing has ended in the first time. How can I release the playing resources normally? How can A superior know when to kill object? At this time, there are two ways: (1) A is disconnected by itself, that is, suicide, and no need to worry about the superiors. In this case, the superiors are quite satisfied and easy to worry about. (2) A tells the superior in A certain way: "After playing, you can kill me. "Although you have to worry about the upper-level, the upper-level processing is also A container, just remove A with A single knife. Scenario 2: Class A is A type of negative enemy. When A superior wants it to die, it cannot die immediately. Instead, it must continue to fulfill its final mission and stop it on its own. For example, A is the sender of A network transmission, and its task is to send something that is handed over to it by the superior to B at the network receiving end. The specific process is as follows: each time the upper-level inserts the thing that needs to be transmitted to B into the buffer zone of A, A just needs to regularly fetch A certain amount of data from the buffer zone, then, send it to B. After transmission for A period of time, the superior found that all the things that need to be passed to B have been inserted to A, so the command A: "You should stop yourself !" Set the suicide variable of A to true ). At this time, A finds that the data in the buffer zone has not been transmitted, so he replied, "Oh, okay, I will commit suicide later ." As A result, when A transfers everything in the buffer zone, it commits suicide.
This situation is very similar to Case 1 above, but the situation is slightly different. Maybe the example is not typical enough. You can try to create a more typical example on your own. Case 3 ]:
Class A encounters A major problem during task execution.) You need to notify the upper-level to kill yourself to ensure that the program runs normally or the resources are released normally. For example, if A is the sender of A network, the task still sends data to the specified destination address B. Originally, this job was quite simple, but some problems occurred on the internet, causing some exceptions when sending data. At this time, A is keenly aware of network exceptions. As A result, B may continue to send data to B, which may cause B to receive some wrong data. Unexpected exceptions may occur. Therefore, A must stop sending data. At this time, A has two options: (1) cheat and cheat. It can close the network transmission connection and throw the data in the sending buffer. It should ensure that the program is safe, but the upper level is very strange. Why did B often tell him why nothing has been sent recently? (2) notify the superior to kill himself. After careful consideration, A must immediately notify the superior to kill himself to ensure the normal release of resources and the normal operation of subsequent work.
Of course, in this case, the idea of suicide must be A major exception. Only by killing yourself can we ensure that, in the future, A will generate the idea of suicide. Summary ]:
We know that there are usually more than one path to the destination. In fact, the various situations described above are similar, and you do not need to use A suicide policy to achieve the ultimate goal. For example, A higher-level thread is opened to regularly ask about the status of, once you find that task A is finished, you can die or A has encountered an exception and want to die. You can immediately kill task. The suicide behavior we discussed in this article is just another path to achieve the goal.
Here, we need to note that we are talking about suicide, which mainly refers to the idea of suicide. There are two ways to achieve this idea. The first is to kill yourself, that is, to "stop yourself "; the other is to notify others to let them kill themselves, that is, "Kill yourself with a knife ".
Next we will discuss how A implements the above-described suicide behavior? Method 1: send a message ]:
This method has some limitations. It can only be used in the framework of MFC. Because MFC implements the message mechanism, we can use this mechanism to convey the underlying idea of suicide to our superiors, then, the superior can kill himself.
For specific usage, see the PostMessage and SendMessage functions of MFC.
This method is "killing yourself with a knife ". Method 2: callback ]:
When an external user registers a callback function to this class, the user can call this callback function to notify the external user when the class object wants to commit suicide, attackers can execute code to kill the object in the callback function.
Callback is not an effective way to implement the notification mechanism in MFC. This method is actually a "kill yourself with a knife ". Method 3: open a thread and kill yourself ]:
This is an advanced method of death, which truly achieves "self-disconnection" without the worry of the outside world. After the class object generates the idea of "suicide", you can immediately start a thread, pass the this pointer to the past, and execute the code to kill the class object in this thread, terminate the object.
When using this method, you need to pay attention to the problem that the outside world does not know that you are dead, and all users must prevent the outside world from using you after your death. Therefore, for the sake of security, before or after the disconnection, you still need to tell the outside world in some way. Method 4: Garbage collection mechanism ]:
The reason for this is that it is indeed a bit like garbage collection. We can have a dedicated thread that will constantly scan the garbage collection bin or a static single-piece class.) Is there any garbage to be cleared? If yes, then, if a Class Object wants to "stop", it will throw itself to the recycle bin through the interface provided by the recycle bin, wait quietly until it is ruthlessly killed.
Similarly, this is also a "self-disconnected" method. You also need to pay attention to the same problems as method 3. Now, how can we commit suicide in a class ?" This question is finally discussed clearly. If you have any comments or have better supplements, please leave a message or send a letter to us ~~

 

This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/232030

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.