Skip to content

Commit 3482ecf

Browse files
committed
Fix weak reference counting
1 parent 7c96d1b commit 3482ecf

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/collections/refcount.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ bool _z_rc_decrease_strong(void** cnt) {
198198
if (_ZP_RC_OP_DECR_AND_CMP_STRONG(c, 1)) {
199199
return _z_rc_decrease_weak(cnt);
200200
}
201-
return _z_rc_decrease_weak(cnt);
201+
_z_rc_decrease_weak(cnt);
202+
return true;
202203
}
203204

204205
bool _z_rc_decrease_weak(void** cnt) {

tests/z_refcount_test.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void test_rc_clone_as_weak(void) {
141141
assert(_z_rc_weak_count(dwk1._cnt) == 2);
142142

143143
assert(dwk1._val->foo == 42);
144-
assert(!_dummy_rc_drop(&drc1));
144+
assert(_dummy_rc_drop(&drc1));
145145
assert(_z_rc_strong_count(dwk1._cnt) == 0);
146146
assert(_z_rc_weak_count(dwk1._cnt) == 1);
147147
assert(_dummy_weak_drop(&dwk1));
@@ -156,7 +156,7 @@ void test_rc_clone_as_weak_ptr(void) {
156156
assert(_z_rc_strong_count(dwk1->_cnt) == 1);
157157
assert(_z_rc_weak_count(dwk1->_cnt) == 2);
158158

159-
assert(!_dummy_rc_drop(&drc1));
159+
assert(_dummy_rc_drop(&drc1));
160160
assert(_z_rc_strong_count(dwk1->_cnt) == 0);
161161
assert(_z_rc_weak_count(dwk1->_cnt) == 1);
162162
assert(_dummy_weak_drop(dwk1));
@@ -180,7 +180,7 @@ void test_weak_clone(void) {
180180
assert(_z_rc_strong_count(dwk2._cnt) == 1);
181181
assert(_z_rc_weak_count(dwk2._cnt) == 3);
182182

183-
assert(!_dummy_rc_drop(&drc1));
183+
assert(_dummy_rc_drop(&drc1));
184184
assert(_z_rc_strong_count(dwk2._cnt) == 0);
185185
assert(_z_rc_weak_count(dwk2._cnt) == 2);
186186

@@ -208,7 +208,7 @@ void test_weak_copy(void) {
208208

209209
void test_weak_upgrade(void) {
210210
_dummy_t val = {.foo = 42};
211-
_dummy_rc_t drc1 = _dummy_rc_new(&val);
211+
_dummy_rc_t drc1 = _dummy_rc_new_from_val(&val);
212212
_dummy_weak_t dwk1 = _dummy_rc_clone_as_weak(&drc1);
213213

214214
// Valid upgrade
@@ -217,7 +217,7 @@ void test_weak_upgrade(void) {
217217
assert(_z_rc_strong_count(drc2._cnt) == 2);
218218
assert(_z_rc_weak_count(drc2._cnt) == 3);
219219
assert(!_dummy_rc_drop(&drc1));
220-
assert(!_dummy_rc_drop(&drc2));
220+
assert(_dummy_rc_drop(&drc2));
221221

222222
// Failed upgrade
223223
_dummy_rc_t drc3 = _dummy_weak_upgrade(&dwk1);
@@ -240,6 +240,10 @@ void test_overflow(void) {
240240

241241
_dummy_weak_t dwk1 = _dummy_rc_clone_as_weak(&drc1);
242242
assert(_Z_RC_IS_NULL(&dwk1));
243+
244+
// Manual free to make asan happy, without long decresing
245+
free(drc1._val);
246+
free(drc1._cnt);
243247
}
244248

245249
void test_decr(void) {
@@ -248,6 +252,7 @@ void test_decr(void) {
248252
_dummy_rc_t drc2 = _dummy_rc_clone(&drc1);
249253
assert(!_dummy_rc_decr(&drc2));
250254
assert(_dummy_rc_decr(&drc1));
255+
free(drc1._val);
251256
}
252257

253258
int main(void) {

0 commit comments

Comments
 (0)