• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用智能指针管理动态分配内存对象
2
3## 概述
4
5智能指针是行为类似指针的类,在模拟指针功能的同时提供增强特性,如针对具有动态分配内存对象的自动内存管理等。
6
7* 自动内存管理主要是指对超出生命周期的对象正确并自动地释放其内存空间,以避免出现内存泄漏等相关内存问题。
8* 智能指针对每一个RefBase对象具有两种不同的引用形式。强引用持有对一个对象的直接引用。 具有强引用关系的对象在该强引用关系存在时同样也应当存在,也即该引用关系有效;弱引用持有对一个对象的间接引用。 具有弱引用关系的对象在该弱引用关系存在时并不保证存在。
9
10> 注意:上述描述仅当正确使用智能指针时才成立。
11
12### 实现原理
13
14* 智能指针通过引用计数来实现所指向对象内存的自动管理。每一个可以被智能指针管理的对象都持有一个引用计数器,引用计数器在相关引用计数清0时会调用一个用于销毁该对象的回调函数。
15
16* 引用计数器记录了对应RefBase对象的两种不同引用计数值,以及对于其本身,即RefCounter对象的引用计数值。
17
18## 涉及功能
19
20### OHOS::sptr
21
22**模块:** **SmartPointer**
23
24指向RefBase(或其子类)对象的强引用智能指针。
25
26#### 具体描述
27
28```cpp
29template <typename T >
30class OHOS::sptr;
31```
32
33指向RefBase(或其子类)对象的强引用智能指针。
34
35**模板参数**:
36
37* **T** 被sptr管理的具体类型。该类必须继承自RefBase基类。
38
39其直接引用RefBase对象。
40
41`#include <refbase.h>`
42
43#### 接口说明
44
45| 返回类型                                    | 名称                                                                               |
46| --------------------------------------- | -------------------------------------------------------------------------------- |
47|                                         | **sptr**()                                                                       |
48| template <typename O \> <br>            | **sptr**(const sptr< O >& other)<br>拷贝构造函数,参数与当前sptr具有不同的管理类型(O)                |
49|                                         | **sptr**(const sptr< T >& other)<br>拷贝构造函数。其以参数指定具体管理对象                         |
50|                                         | **sptr**(sptr< T >&& other)<br>移动构造函数                                           |
51|                                         | **sptr**(T* other)<br>构造函数。其以参数指定具体管理对象                                         |
52|                                         | **sptr**(WeakRefCount* p, bool force)<br>构造函数。仅用于wptr的promote操作                 |
53|                                         | **~sptr**()                                                                      |
54| void                                    | **clear**()<br>移除当前sptr与所管理对象的引用关系                                               |
55| void                                    | **ForceSetRefPtr**(T* other)<br>强制更改被管理对象指针的指向                                  |
56| T*                                     | **GetRefPtr**() const<br>获取sptr管理对象的指针                                           |
57|                                         | **operator T***() const<br>类型转换运算符                                               |
58| bool                                    | **operator!**() const<br>逻辑非运算符。检查sptr对象是否为空sptr对象                               |
59| bool                                    | **operator!=**(const sptr< T >& other) const<br>sptr对象间的不等运算符                   |
60| bool                                    | **operator!=**(const T* other) const<br>sptr对象与裸指针间的不等运算符                       |
61| bool                                    | **operator!=**(const wptr< T >& other) const<br>sptr对象与wptr间的相等运算符               |
62| T&                                     | **operator***() const<br>解引用运算符,其会返回wptr管理的RefBae对象                              |
63| T*                                     | **operator->**() const<br>成员选择运算符,其将会返回被sptr管理对象的指定成员                            |
64| template <typename O \> <br>sptr< T >& | **operator=**(const sptr< O >& other)<br>拷贝赋值运算符,参数为一个sptr对象,但与当前sptr对象具有不同管理类型 |
65| sptr< T >&                             | **operator=**(const sptr< T >& other)<br>拷贝赋值运算符,参数与当前sptr对象具有相同管理类型            |
66| sptr< T >&                             | **operator=**(const wptr< T >& other)<br>拷贝赋值运算符,参数为一个wptr对象,但与当前sptr对象具有相同管理类型  |
67| sptr< T >&                             | **operator=**(sptr< T >&& other)<br>移动构造运算符                                     |
68| sptr< T >&                             | **operator=**(T* other)<br>拷贝赋值运算符,参数为待管理的具体对象                                  |
69| bool                                    | **operator==**(const sptr< T >& other) const<br>sptr对象间的相等运算符                   |
70| bool                                    | **operator==**(const T* other) const<br>sptr对象与裸指针间的相等运算符                       |
71| bool                                    | **operator==**(constwptr< T >& other) const<br>sptr对象与wptr间的相等运算符               |
72
73### OHOS::wptr
74
75**模块:** **SmartPointer**
76
77指向RefBase(或其子类)对象的弱引用智能指针。
78
79#### 具体描述
80
81```cpp
82template <typename T >
83class OHOS::wptr;
84```
85
86指向RefBase(或其子类)对象的弱引用智能指针。
87
88**模板参数**:
89
90* **T** 被wptr管理的具体类型。该类必须继承自RefBase基类。
91
92其间接引用RefBase对象;直接引用WeakRefCounter对象。
93
94`#include <refbase.h>`
95
96#### 接口说明
97
98| 返回类型                                    | 名称                                                                                   |
99| --------------------------------------- | ------------------------------------------------------------------------------------ |
100|                                         | **wptr**()                                                                           |
101| template <typename O \> <br>            | **wptr**(const sptr< O >& other)<br>拷贝构造函数。参数为一个sptr对象,且与当前wptr对象具有不同的管理类型(O)       |
102|                                         | **wptr**(const sptr< T >& other)<br>拷贝构造函数。参数为一个sptr对象,但与当前wptr对象具有相同的管理类型          |
103| template <typename O \> <br>            | **wptr**(const wptr< O >& other)<br>拷贝构造函数。参数与当前wptr对象具有不同的管理类型                      |
104|                                         | **wptr**(const wptr< T >& other)<br>拷贝构造函数。参数与当前wptr对象具有相同的管理类型                      |
105|                                         | **wptr**(T* other)<br>构造函数。其以参数指定具体管理对象                                             |
106|                                         | **~wptr**()                                                                          |
107| bool                                    | **AttemptIncStrongRef**(const void* objectId) const<br>尝试对被管理对象的强引用计数加一             |
108| T*                                     | **GetRefPtr**() const<br>获取指向被管理RefBase对象的指针                                         |
109| bool                                    | **operator!=**(const sptr< T >& other) const<br>wptr与输入sptr对象间的不等运算符                |
110| bool                                    | **operator!=**(const T* other) const<br>wptr对象与裸指针间的不等运算符                           |
111| bool                                    | **operator!=**(constwptr< T >& other) const<br>wptr对象间的不等运算符                        |
112| T&                                     | **operator***() const<br>解引用运算符,其会返回wptr管理的RefBae对象                                  |
113| T*                                     | **operator->**() const<br>成员选择操作符,其将会返回被wptr管理对象的指定成员                                |
114| template <typename O \> <br>wptr< T >& | **operator=**(const sptr< O >& other)<br>拷贝赋值运算符,参数为一个sptr对象,但与当前wptr对象具有不同的管理类型(O) |
115| wptr< T >&                             | **operator=**(const sptr< T >& other)<br>拷贝赋值运算符,参数为一个sptr对象,但与当前wptr对象具有相同的管理类型(T) |
116| template <typename O \> <br>wptr< T >& | **operator=**(const wptr< O >& other)<br>拷贝赋值运算符,参数为一个wptr对象,但与当前wptr对象具有不同的管理类型(O)  |
117| wptr< T >&                             | **operator=**(const wptr< T >& other)<br>拷贝赋值运算符,参数为一个wptr对象,且与当前wptr对象具有相同的管理类型(T)  |
118| template <typename O \> <br>wptr< T >& | **operator=**(O* other)<br>拷贝赋值运算符,参数为待管理的具体对象                                      |
119| wptr< T >&                             | **operator=**(T* other)<br>拷贝赋值运算符,参数为待管理的具体对象                                      |
120| bool                                    | **operator==**(const sptr< T >& other) const<br>wptr与输入sptr对象间的相等运算符                |
121| bool                                    | **operator==**(const T* other) const<br>wptr对象与裸指针间的相等运算符                           |
122| bool                                    | **operator==**(const wptr< T >& other) const<br>wptr对象间的相等运算符                        |
123| const sptr< T >                         | **promote**() const<br>将该wptr提升为sptr                                                   |
124
125## 使用示例
126
1271. 使用方法(伪代码)
128
129```
130#include "../include/refbase.h"
131#include <iostream>
132
133using namespace std;
134using namespace OHOS;
135
136// 管理目标类
137class RefBaseTest : public RefBase {
138public:
139    virtual void Access()
140    {
141        cout<<"Access RefBaseTest::Show"<<endl;
142    }
143
144    ~RefBaseTest() override
145    {
146        cout << "RefBaseTest destroyed" << endl;
147    }
148};
149
150// 管理目标类的子类
151class SubRefBaseTest : public RefBaseTest {
152public:
153    void Access() override
154    {
155        cout<<"Access SubRefBaseTest::Show"<<endl;
156    }
157
158    ~SubRefBaseTest() override
159    {
160        cout << "SubRefBaseTest destroyed" << endl;
161    }
162};
163
164int main()
165{
166    // 1. 使用新创建智能指针,管理新创建对象
167    sptr<RefBaseTest> newSptr(new RefBaseTest());
168    wptr<RefBaseTest> newWptr(new RefBaseTest());
169
170    // 2. 使用上述智能指针,管理另一个新创建对象
171    // 原管理对象析构
172    newSptr = new RefBaseTest();
173    newWptr = new RefBaseTest();
174
175    // 3. 使用新创建智能指针,指向其他现存智能指针管理对象
176    sptr<RefBaseTest> curSptr(newSptr);
177    wptr<RefBaseTest> curWptr(newWptr);
178
179    if (curSptr->GetSptrRefCount() == 2 && curSptr->GetWptrRefCount() == 2 && // 2: count
180       curWptr->GetWptrRefCount() == 1) {
181        cout << "curSptr run as expected" << endl;
182    }
183
184    // 4. 使用现存智能指针管理其托管类型的子类对象
185    sptr<SubRefBaseTest> subSptr(new SubRefBaseTest());
186    wptr<SubRefBaseTest> subWptr(new SubRefBaseTest());
187
188    curSptr = subSptr;
189    curWptr = subWptr;
190
191    // 5. 通过->运算符访问成员"
192    curSptr->Access();
193    curWptr->Access();
194
195    // 6. 通过*运算符解引用
196    (*curSptr).Access();
197    (*curSptr).Access();
198
199    // 7. 两种智能指针可以管理对方所管理的对象
200    sptr<RefBaseTest> scurSptr(new RefBaseTest);
201    wptr<RefBaseTest> scurWptr(new RefBaseTest);
202
203    wptr<RefBaseTest> snewWptr(scurSptr);
204
205    sptr<RefBaseTest> soldSptr(new RefBaseTest);
206    wptr<RefBaseTest> soldWptr(new RefBaseTest);
207    soldSptr = scurWptr; // sptr仅可通过拷贝赋值管理wptr所管理对象
208    soldWptr = scurSptr; // 原本的引用关系将被释放
209
210    if (snewWptr->GetWptrRefCount() == 3 && soldSptr->GetSptrRefCount() == 1 && // 3: count
211        soldWptr->GetWptrRefCount() == 3) { // 3: count
212            cout << "Smart Pointer assignment run as expected" << endl;
213        }
214    // 8. wptr可升级为sptr
215    sptr<RefBaseTest> spromotedWptr = snewWptr.promote(); // 升级失败时返回空sptr对象,即未管理具体对象的sptr对象
216    if (spromotedWptr->GetSptrRefCount() == 2 && spromotedWptr->GetWptrRefCount() == 4) { // 2, 4: count
217        cout << "Promote run as expected" << endl;
218    }
219
220    return 0;
221}
222```
223
2242. 测试用例编译运行方法
225
226- 测试用例代码参见 base/test/unittest/common/utils_refbase_test.cpp
227
228- 使用开发者自测试框架,使用方法参见:[开发自测试执行框架-测试用例执行](https://gitee.com/openharmony/testfwk_developer_test#%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E6%89%A7%E8%A1%8C)
229
230- 使用以下具体命令以运行`refbase.h`对应测试用例
231
232```bash
233run -t UT -tp utils -ts UtilsRefBaseTest
234```
235
2363. debug功能
237RefTracker作为debug工具被添加入refbase文件中,以便开发者对RefBase相关问题进行定位。该功能需要重新编译动态库替换系统原有动态库来上机使用(如是静态依赖则需开发者独立审视使能方法)。
238- 全局追踪
239
240全局追踪功能通过编译宏控制,可以追踪全局的RefBase及其子类的轨迹,但同时会对整机性能造成影响。
241全局追踪中我们提供了立即打印模式及非立即打印模式。立即打印模式会在每次引用计数发生变化时对计数进行打印。非立即打印模式会在RefBase及其子类对象析构时对轨迹进行打印。
242
243全局追踪、立即打印编译命令:
244```
245./build.sh --product-name xxx --ccache --build-target commonlibrary/c_utils/base:utils --gn-args c_utils_debug_refbase=true --gn-args c_utils_track_all=true --gn-args c_utils_print_track_at_once=true
246```
247全局追踪、非立即打印编译命令:
248```
249./build.sh --product-name xxx --ccache --build-target commonlibrary/c_utils/base:utils --gn-args c_utils_debug_refbase=true --gn-args c_utils_track_all=true
250```
251- 独立追踪
252
253独立追踪功能同样能通过编译宏控制。我们为开发者提供了RefBase::EnableTracker()接口来对某个具体实例使能追踪功能。独立追踪对性能影响很小,可以忽略不计。在独立追踪中我们能同样提供了立即打印及非立即打印模式。
254
255独立追踪、立即打印编译命令:
256```
257./build.sh --product-name xxx --ccache --build-target commonlibrary/c_utils/base:utils --gn-args c_utils_debug_refbase=true --gn-args c_utils_print_track_at_once=true
258```
259独立追踪、非立即打印编译命令:
260```
261./build.sh --product-name xxx --ccache --build-target commonlibrary/c_utils/base:utils --gn-args c_utils_debug_refbase=true
262```
263
264- 使用方法
265
266编译动态库,编译产物路径为`./out/xxx/commonlibrary/c_utils/libutils.z.so`。
267
268编译产物需要推入系统进行替换,64位系统位于`/system/lib64/`,32位系统位于`/system/lib/`。
269
270追踪结果通过log打印。格式如下:
271```
272// 立即打印
273(sptr pointer) start tracking
274(sptr pointer) call (RefBase pointer). strong: x weak: x refcnnt: x
275...
276(sptr pointer) call (RefBase pointer). strong: x weak: x refcnnt: x
277(sptr pointer) end tracking
278
279// 非立即打印
280(sptr pointer) start backtrace
281(sptr pointer) call (RefBase pointer). strong: x weak: x refcnnt: x PID: xxx TID: xxx
282...
283(sptr pointer) call (RefBase pointer). strong: x weak: x refcnnt: x PID: xxx TID: xxx
284(sptr pointer) end backtrace
285```
286
287## 常见问题
288
2891. **使用本实现智能指针时,同时使用裸指针或std标准库智能指针(std::shared_ptr)**
290
291   * 会造成管理冲突,导致非法访问以及未定义行为,如内存重复释放。
292     * 因此也不推荐先创建裸指针后,再使用智能指针管理。
293```c++
294RefBase* a = new RefBase();
295sptr<RefBase> s = a;
296// 或
297sptr<RefBase> s(a); // 裸指针a容易被误delete,造成sptr功能失常
298```
299
3002. **智能指针需构造在栈上,管理的对象需要在堆上(动态分配对象)**
301
302   * 智能指针若构造在堆上则不符合定义。
303   * 管理对象若构造在栈上,则会自动释放,错误绕开智能指针管控。
304
3053. **智能指针不保证线程安全**,使用者需保证线程安全以避免同时对同一个sptr对象赋值等操作
306
3074. **避免通过隐式转换构造智能指针对象**
308
309   * 易造成误解。
310   * 因编译器优化程度具有不确定的行为,易造成问题。
311