feat(kernel/environment): make the environment throw an exception when weak-ref has expired
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
590b14570f
commit
4ebb3c572a
3 changed files with 20 additions and 1 deletions
|
@ -435,6 +435,12 @@ environment::environment(std::shared_ptr<imp> const & ptr):
|
|||
m_ptr(ptr) {
|
||||
}
|
||||
|
||||
environment::environment(weak_ref const & r) {
|
||||
if (r.expired())
|
||||
throw exception("weak reference to environment object has expired (i.e., the environment has been deleted)");
|
||||
m_ptr = r.lock();
|
||||
}
|
||||
|
||||
environment::~environment() {
|
||||
}
|
||||
|
||||
|
|
|
@ -282,6 +282,6 @@ public:
|
|||
public:
|
||||
typedef std::weak_ptr<imp> weak_ref;
|
||||
weak_ref to_weak_ref() const { return weak_ref(m_ptr); }
|
||||
environment(weak_ref const & r):m_ptr(r) { lean_assert(!r.expired()); }
|
||||
environment(weak_ref const & r);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -270,6 +270,18 @@ static void tst12() {
|
|||
lean_assert(ext.get_parent<my_extension>() == nullptr);
|
||||
}
|
||||
|
||||
static void tst13() {
|
||||
environment::weak_ref wref;
|
||||
{
|
||||
environment env;
|
||||
wref = env.to_weak_ref();
|
||||
}
|
||||
try {
|
||||
environment env2(wref);
|
||||
lean_unreachable();
|
||||
} catch (exception &) {}
|
||||
}
|
||||
|
||||
int main() {
|
||||
enable_trace("is_convertible");
|
||||
tst1();
|
||||
|
@ -284,5 +296,6 @@ int main() {
|
|||
tst10();
|
||||
tst11();
|
||||
tst12();
|
||||
tst13();
|
||||
return has_violations() ? 1 : 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue