40
40
* // Increase reference count
41
41
* SharedPtr<MyStruct> ptr2( ptr );
42
42
*
43
- * ptr =
44
- *
45
- * BLE& ble_interface = BLE::Instance();
43
+ * ptr = NULL; // Reference to the struct instance is still held by ptr2
44
+ *
45
+ * ptr2 = NULL; // The raw pointer is freed
46
46
* @endcode
47
47
*
48
48
*
@@ -73,10 +73,12 @@ class SharedPtr {
73
73
* @brief Create new SharedPtr
74
74
* @param ptr Pointer to take control over
75
75
*/
76
- SharedPtr (T* ptr): _ptr(ptr) {
76
+ SharedPtr (T* ptr): _ptr(ptr), _counter( NULL ) {
77
77
// allocate counter on the heap so it can be shared
78
- _counter = (uint32_t *) malloc (sizeof (uint32_t ));
79
- *_counter = 1 ;
78
+ if (_ptr != NULL ) {
79
+ _counter = (uint32_t *) malloc (sizeof (uint32_t ));
80
+ *_counter = 1 ;
81
+ }
80
82
}
81
83
82
84
/* *
@@ -95,7 +97,7 @@ class SharedPtr {
95
97
*/
96
98
SharedPtr (const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) {
97
99
// increment reference counter
98
- if (_counter ) {
100
+ if (_ptr != NULL ) {
99
101
core_util_critical_section_enter ();
100
102
(*_counter)++;
101
103
core_util_critical_section_exit ();
@@ -115,10 +117,10 @@ class SharedPtr {
115
117
116
118
// assign new values
117
119
_ptr = source.get ();
118
- _counter = source.getCounter ();
120
+ _counter = source.get_counter ();
119
121
120
122
// increment new counter
121
- if (_counter ) {
123
+ if (_ptr != NULL ) {
122
124
core_util_critical_section_enter ();
123
125
(*_counter)++;
124
126
core_util_critical_section_exit ();
@@ -128,6 +130,28 @@ class SharedPtr {
128
130
return *this ;
129
131
}
130
132
133
+ /* *
134
+ * @brief Replaces the managed pointer with a new unmanaged pointer.
135
+ * @param[in] ptr the new raw pointer to manage.
136
+ */
137
+ void reset (T* ptr) {
138
+ // clean up by decrementing counter
139
+ decrement_counter ();
140
+
141
+ if (ptr != NULL ) {
142
+ // allocate counter on the heap so it can be shared
143
+ _counter = (uint32_t *) malloc (sizeof (uint32_t ));
144
+ *_counter = 1 ;
145
+ }
146
+ }
147
+
148
+ /* *
149
+ * @brief Replace the managed pointer with a NULL pointer.
150
+ */
151
+ void reset () {
152
+ reset (NULL );
153
+ }
154
+
131
155
/* *
132
156
* @brief Raw pointer accessor.
133
157
* @details Get raw pointer to object pointed to.
@@ -142,7 +166,7 @@ class SharedPtr {
142
166
* @return Reference count.
143
167
*/
144
168
uint32_t use_count () const {
145
- if (_counter ) {
169
+ if (_ptr != NULL ) {
146
170
core_util_critical_section_enter ();
147
171
return *_counter;
148
172
core_util_critical_section_exit ();
@@ -172,15 +196,15 @@ class SharedPtr {
172
196
* @return Whether or not the pointer is NULL.
173
197
*/
174
198
operator bool () const {
175
- return (_ptr != 0 );
199
+ return (_ptr != NULL );
176
200
}
177
201
178
202
private:
179
203
/* *
180
204
* @brief Get pointer to reference counter.
181
205
* @return Pointer to reference counter.
182
206
*/
183
- uint32_t * getCounter () const {
207
+ uint32_t * get_counter () const {
184
208
return _counter;
185
209
}
186
210
@@ -194,7 +218,9 @@ class SharedPtr {
194
218
if (*_counter == 1 ) {
195
219
core_util_critical_section_exit ();
196
220
free (_counter);
221
+ _counter = NULL ;
197
222
delete _ptr;
223
+ _ptr = NULL ;
198
224
} else {
199
225
(*_counter)--;
200
226
core_util_critical_section_exit ();
0 commit comments