Strong and weak pointers to OSG smart pointers

Source: Internet
Author: User

People who use OSG know that OSG's memory management method uses smart pointers to allow OSG to handle the destruction of objects by itself. There are two smart pointer types in OSG. One is the ref_ptr that we are not familiar with. The other may not be often used by many people, but it is indeed a very good observer_ptr. These two types appear as smart pointers. What are their differences? Why are there two similar things? Next we will unveil them.
In fact, they are two smart pointer types, one is the familiar strong pointer type (strong pointer), the other is the weak pointer type (weak pointer), and The ref_ptr is the strong pointer type, observer_ptr is a weak pointer type. The strong pointer type ref_ptr is the same as the smart pointer type in general sense. The reference count is used to record how many objects are using this object, when no object uses this object, the object is automatically destroyed, and observer_ptr also points to an object, but it does not reference the counting function, it only records the address of this object, when this object is destroyed elsewhere, the object address obtained through observer_ptr will be blank. Why is it a weak pointer? I don't know how the author thinks about this concept. I personally think it is because it is only a record of the object address and does not participate in object lifetime management, so it is called weak. I don't know if you think so. I think so anyway, haha.
At this point, we should have doubts. Why do we need multiple weak smart pointers? Isn't it a matter of being mentally retarded? Isn't a ref_ptr enough? I want to talk about my opinion. I don't know if it is accurate. It only represents my opinion. If we only use ref_ptr, it means that in any situation, as long as we need to reference an object, it is necessary to increase its reference count, and reduce the reference count if it is not used. The first problem we need to clarify is that if the number of objects is very large, frequent increase of reference count and reduction of reference count will reduce some new capabilities, which should be beyond doubt. In addition, in many cases, we only need to reference the object for convenient access during the lifetime of the object. If ref_ptr is used to increase its reference count, the lifetime of the object will be changed, we need to control the object's survival through additional means, which will inevitably increase the complexity of the system. For example, we have created several planes in the system, at the same time, the system has an aircraft manager that manages all aircraft in a unified manner. The normal logic is that when an airplane explodes, we will destroy the aircraft object. If we use ref_ptr in the aircraft manager, after the aircraft is removed from reference elsewhere, the aircraft object will not be automatically destroyed, because the aircraft manager still has reference to it, and the aircraft must be manually taken by some means of judgment. The reference count of the aircraft object is reduced to 0 after the object is removed from the aircraft manager, and the object is destroyed. So far, have we done a thankless task? If observer_ptr is used, we will not be troubled by this because it does not interfere with the object's survival, and will not reduce the efficiency of reference counting operations. This is why I understand the weak smart pointer, which may be inaccurate or not comprehensive. You are welcome to revise it.
Next, let's take a look at how observer_ptr references objects and how to know this situation when the object is destroyed. In fact, it is very simple. We can guess from the name that the observer mode is used here, and the observer is automatically notified after the object is destroyed. The following code shows if this is the case.
First of all, we must look at the constructor of the smart pointer. It has several constructor functions, so we can use the most common and typical ones. The others are similar.
Observer_ptr (T * RP)
{
_ Reference = RP? RP-> getorcreateobserverset (): 0;
_ PTR = (_ reference. Valid () & _ reference-> getobserverdobject ()! = 0 )? RP: 0;
}
This is constructed like ref_ptr through an original object. One thing in the constructor is to initialize the member variables. There are two member variables in total, one is the object called the observer set, and the other is the pointer of the original object referenced by the smart pointer, just like the ref_ptr. The focus here is the member of the observer set, which is actually an observer of the object. When the object is destroyed elsewhere, the observer will be notified.
In fact, the above RP, _ PTR, and _ reference-> getobserverdobject () are one thing. Let's think, if they are different, they will be finished? Because what we want to reference and observe must be something! Any subclass inherited from the referenced class can have an observer set. An observer set observes an object, and a set can have several observers, which does not involve the observer in the Observer set, it only involves the object to be observed, so we will not talk about it to prevent interference. If you are interested, you can read the code. RP-> getorcreateobserverset () is to create an observer set for the object referenced by the smart pointer. When creating an observer set, the object naturally tells the observer set to observe itself. Do not believe it? Code:
Observerset * referenced: getorcreateobserverset () const
{
......
Observerset * newobserverset = new observerset (this );
......
}
Now you should understand how RP, _ PTR, and _ reference-> getobserverdobject () are a thing.
Well, we already know how observer_ptr observes the referenced object. How does it know when the object is destroyed? What? Do you believe it knows? Believe it or not, I believe it. Check the Code:
Void referenced: signalobserversanddelete (bool signaldelete, bool dodelete) const
{
......
If (observerset & signaldelete)
{
Observerset-> signalobjectdeleted (const_cast <referenced *> (this ));
}
......
}

Void observerset: signalobjectdeleted (void * PTR)
{
......
_ Observedobject = 0;
}

As you can see, when the referenced object is destroyed, it will tell the observer set: the object you observe does not exist: _ observedobject = 0;
At this point, it will be connected as soon as you go back. It's almost over. Don't worry. Let's look at another line of code:
Inline T * Get () const {return (_ reference. Valid () & _ reference-> getobserverdobject ()! = 0 )? _ PTR: 0 ;}
What did you see? What is returned if the object is destroyed? Null! You are correct. Don't forget that it does not increase the reference count of the referenced object, and does not modify the lifetime of the referenced object, this means that the referenced object may have been destroyed elsewhere when you use it. Therefore, you must not forget to judge it when using it. Otherwise, it is easy to "Abort!" .
Are you finished? Oh, no. What else should I do if the object is destroyed in another thread during use? Oh, yes. What should I do? Isn't it necessary to use ref_ptr? Yes, it's time to use ref_ptr. Otherwise, if you are on the roof, the bastard will remove the ladder and you will die. Use ref_ptr! But it doesn't mean you don't need to use ref_ptr to discard observer_ptr. I will tell you what to use by looking at the Code:
Bool lock (ref_ptr <t> & rptr) const
{
If (! _ Reference)
{
Rptr = 0;
Return false;
}

Referenced * OBJ = _ reference-> addreflock ();
If (! OBJ)
{
Rptr = 0;
Return false;
}

Rptr = _ PTR;
OBJ-> unref_nodelete ();
Return rptr. Valid ();
}
{
Rptr = 0;
Return false;
}

Referenced * OBJ = _ reference-> addreflock ();
If (! OBJ)
{
Rptr = 0;
Return false;
}

Rptr = _ PTR;
OBJ-> unref_nodelete ();
Return rptr. Valid ();
}
This time, you understand what to do if you realize that there is such a hidden risk? I will not translate it. Read two official notes:
The comment of this function is:
/**
* Assign the observer_ptr to a ref_ptr. The ref_ptr will be valid if
* Referenced object hasn' t been deleted and has a ref count> 0.
*/

Notes for observer_ptr:
* If you are in any doubt about whether it is safe to access the object safe then use
* Ref_ptr <> observer_ptr <>. Lock () combination .*/
All done!

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.