Preface
Since Microsoft announced the COM technology for the first time in 1993, the development model on the Windows platform has undergone great changes. A series of software componentization technologies based on COM have brought Windows programming into the componentization era. Until now, COM technology is still thriving, and a large number of components and software have been reused and shared with resources through COM technology.
Due to technical complexity, COM programming was once regarded as an unattainable technology, which was daunting. While pushing Windows to the componentization era, COM also pushed programmers who developed and used COM components to another dangerous summit. There are unattainable traps and xuanjicang hidden everywhere, and they will be broken down with a slight carelessness.
Now, imagine that you have reached the summit of COM for you. The four sides are the cliff. If you are not careful with one step, it will fall to the bottom of the abyss, and your life will be suspended. If I put a parachute behind you at this time, I think you will not refuse.
The smart pointer of the COM interface is a "parachute" that solves the reference counting problem in COM technology ". Although he cannot guarantee that you will not fall down the cliff, it will never be ugly.
Maybe the reason why many people refuse to use the smart pointer of the COM interface is the same as that for rejecting the "parachute:
1. You need to learn how to use it correctly. (If You Need To Know when to open your parachute );
2. You need to avoid the negative effects it brings and avoid new risks it introduces. (You have to know how you can not be hung on a tree or blown to another cliff after you pull the parachute );
After understanding this, I think you are willing to use smart pointers instead of the original manual reference count. If you think that the original manual operation reference count makes the code clearer and clearer. You may be troubled by problems such as "abnormal security" and "Type Security. Unfortunately, manual counting does not make the code clearer. In the first chapter of this article, you will see this content.
I once heard people say, "Don't use smart pointers, because sometimes you don't know whether they are smart pointers or interface pointers ." This idea really makes sense, because we want the program to be as clear and understandable as possible. Smart pointers can hide many implementation details. If you are not familiar with him, you may not know what kind of "intelligent" operation he has done at a certain time point. However, if you use this article, you can understand the implementation details and take corresponding solutions accordingly. It seems much more valuable than simply rejecting it.
This article focuses on the use and design of smart pointers. We will discuss how smart pointers can deal with certain problems, and try to parse some historical and platform backgrounds as much as possible, as well as some design principles that he complies. I also found some guiding ideology that can guide our future design. I organized these ideas in the form of chapters and clauses.
In chapter 1, you will be able to have a rough understanding of the use of COM interface smart pointers. And understand the terms and rules that smart pointer needs to comply.
In chapter 2, we discuss several issues about the use of smart pointers in rules. It tells you how to use smart pointers correctly without downgrading to the memory and resource leakage dilemma.
In chapter 3, we discuss how to design a set of principles that smart pointers need to follow and how to choose between these principles when conflicts occur.
In chapter 4, we will discuss the details of the specific implementation. We will discuss how to write an interface, and how to use some techniques to make it difficult to misuse the designed interface, and various details in the smart pointer writing process.
The last chapter is about some miscellaneous issues. These things are often controversial, so some smart pointer designers will use it, while others will not. We look at these problems with a neutral attitude and provide a solution for native music.
Through this document, I try my best to explore some of the problems hidden behind Smart pointers, so that we can use them more effectively and correctly design the COM smart pointers suitable for our own projects. I have compiled a large number of examples to illustrate some points of view (some of them are adapted from other papers, and I have made a famous place ). I will analyze the problems and causes in these examples one by one, hoping that the readers will have an open-minded effect after reading them.
Due to the limited editor level, there may be errors in many aspects of this article. I also hope readers can correct me a lot.
Author's "liuchang5 column"