From 536b9c4ec289317ef6d611e89a23397041bfe4e6 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Fri, 24 Dec 2010 11:40:59 +0100
Subject: [PATCH] fight the symptoms of BZDNG-154

Not a complete fix: Accessing AtomHandles, ResidueHandles and ChainHandles whose
EntityHandle is out of scope still crashes. That requires larger refactoring
though.
---
 modules/mol/base/src/atom_view.cc    | 5 ++++-
 modules/mol/base/src/chain_view.cc   | 6 +++++-
 modules/mol/base/src/residue_view.cc | 5 ++++-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/modules/mol/base/src/atom_view.cc b/modules/mol/base/src/atom_view.cc
index 5811a2d55..20c14afbe 100644
--- a/modules/mol/base/src/atom_view.cc
+++ b/modules/mol/base/src/atom_view.cc
@@ -67,7 +67,10 @@ AtomView::AtomView(const ResidueView& residue_view,
 ResidueView AtomView::GetResidue() const 
 {
   this->CheckValidity();
-  return ResidueView(data_->residue.lock(), Impl()->GetResidue());
+  if (!data_->residue.expired()) {
+    return ResidueView(data_->residue.lock(), Impl()->GetResidue());    
+  }
+  throw InvalidHandle();
 }
 
 void AtomView::Apply(EntityVisitor& visitor) 
diff --git a/modules/mol/base/src/chain_view.cc b/modules/mol/base/src/chain_view.cc
index 0e6458c38..e5d7e0549 100644
--- a/modules/mol/base/src/chain_view.cc
+++ b/modules/mol/base/src/chain_view.cc
@@ -79,7 +79,11 @@ ChainView::ChainView() {
 }
 
 EntityView ChainView::GetEntity() const {
-  return EntityView(data_->entity.lock(), Impl()->GetEntity());
+  this->CheckValidity();
+  if (!data_->entity.expired()) {
+    return EntityView(data_->entity.lock(), Impl()->GetEntity());    
+  }
+  throw InvalidHandle();
 }
 
 ChainView::ChainView(const EntityView& entity,
diff --git a/modules/mol/base/src/residue_view.cc b/modules/mol/base/src/residue_view.cc
index 8f76c5766..2fef9c943 100644
--- a/modules/mol/base/src/residue_view.cc
+++ b/modules/mol/base/src/residue_view.cc
@@ -138,7 +138,10 @@ const AtomViewList& ResidueView::GetAtomList() const {
 
 ChainView ResidueView::GetChain() const {
   this->CheckValidity();
-  return ChainView(data_->chain.lock(), Impl()->GetChain());
+  if (!data_->chain.expired()) {
+    return ChainView(data_->chain.lock(), Impl()->GetChain());
+  }
+  throw InvalidHandle();
 }
 
 
-- 
GitLab