(To) addref () in the COM Component ()

Source: Internet
Author: User

D3d is a COM component that runs in a service process rather than the current client process. When the DX component is running, you need to create a series of interface objects, such as createdevice (), which return interface pointers. When will these interfaces and their memory usage be released, it should be solved through the "reference counting" technology. Addref () adds 1 to the counter of this interface pointer, and release () will subtract 1 from it. Once it is reduced to 0, it indicates that no Customer is using it, and the related interfaces are released. Therefore, after calling Rlease (), the memory is not necessarily released, but is released when the reference count is 0.

 
In this way, the use of interface pointers should be as careful as maintaining the balance of the stack, and should be used according to some agreed rules.

 

But in d3d programming, how do I not use addref? This is because an interface pointer, such as the id3ddevice or vertexbuf pointer, is issued by d3dxcreate. During the create process, the internal address is already included (), so you do not need to do this again. If you do not use it, call the P pointer-> relase () and release it. General programming, especially small sample programs, is created once during initialization and released when it is closed, all follow this convention, so this problem does not exist.

 

However, in the createmeshcontainer () function, pointers are used in multiple ways and passed back and forth in local pointer variables, which complicate the problem. In COM programming, it is agreed that addref () is required to assign a value (copy) to the local interface pointer at any time, and release () is performed before the life cycle of the pointer variable ends (). but many programmers do not strictly do this. Because the local variables are used up, addref () increases the count and then release () reduces, which is equivalent to directly using the last. This is almost the case. This is related to programming habits. If the reference count is incorrect, it is difficult to troubleshoot if there is no uniform habit. In createmeshcontainer (), there are three methods to use interface pointers:

 

Method 1: Do not use addref (). Like a normal pointer, the temporary variable is the left value and the interface pointer is the right value. For example:
Pmesh = pmeshdata-> pmesh;
This is because pmesh is a local variable. It is just a temporary reference, and there is no need to addref () For it first, and then release ().

 

Method 2: Use addref () implicitly (). Because some functions with the addref () action are used, the release ()
Pmesh-> getdevice (& pd3ddevice); // here, the reference count of the d3d device has been increased by 1.
....
Safe_release (pd3ddevice); // -- in this example, the reference count is reduced by 1, not the d3d device.
In this example, pd3ddevice has passed addref () in getdevice (). Therefore, pd3ddevice-> release () is required before exiting createmeshcontainer ()

 

Method 3: Explicitly uses addref (). If a pointer value is not generated by d3dxcreate, It is copied to a global or long-term variable by assigning a value. Therefore, you can use addref () to delay the release of this object. Because, if you do not add (), it is very likely that the object will be released when the function returns. It is like a gas station, so that the life of the input object is extended to its own control. If addref () is used, you must add release () to the related destroy ().

(To) addref () in the COM Component ()

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.