From b8cc468f0294a3ecc345a48ceb384a08eff1456f Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Wed, 10 Oct 2018 21:04:23 +0200 Subject: [PATCH] base: support assignment of invalid weak_ptr This fixes the region-map component implementation in core, which uses a 'Genode::Weak_ptr _faulting_region_map' member. This member is assigned a valid weak_ptr or an invalid weak_ptr according to the state machine. Fixes #3012 --- repos/base/include/base/weak_ptr.h | 4 +--- repos/base/src/test/weak_ptr/main.cc | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/repos/base/include/base/weak_ptr.h b/repos/base/include/base/weak_ptr.h index dca659f312..52bbbd7980 100644 --- a/repos/base/include/base/weak_ptr.h +++ b/repos/base/include/base/weak_ptr.h @@ -368,11 +368,9 @@ struct Genode::Locked_ptr : Genode::Locked_ptr_base void Genode::Weak_ptr_base::_adopt(Genode::Weak_object_base *obj) { - if (!obj) - return; - _obj = obj; + if (_obj) { Lock::Guard guard(_obj->_list_lock); _obj->_list.insert(this); diff --git a/repos/base/src/test/weak_ptr/main.cc b/repos/base/src/test/weak_ptr/main.cc index ca93a5f204..3a7a170ec4 100644 --- a/repos/base/src/test/weak_ptr/main.cc +++ b/repos/base/src/test/weak_ptr/main.cc @@ -121,6 +121,22 @@ static void test_weak_pointer_tracking(Genode::Heap & heap) log("test: assign weak pointer to another"); Weak_ptr ptr_3 = ptr_2; assert_weak_ptr_cnt(obj, 3); + assert_weak_ptr_valid(ptr_3, true); + + log("test: destruct weak pointer"); + /* 'ptr_3' gets destructed when leaving the scope */ + } + assert_weak_ptr_cnt(obj, 2); + + { + log("test: assign invalid weak pointer to another"); + Weak_ptr ptr_3 = ptr_2; + assert_weak_ptr_cnt(obj, 3); + assert_weak_ptr_valid(ptr_3, true); + + ptr_3 = Weak_ptr(); + assert_weak_ptr_cnt(obj, 2); + assert_weak_ptr_valid(ptr_3, false); log("test: destruct weak pointer"); /* 'ptr_3' gets destructed when leaving the scope */