<rdar://problem/11184458>

Found an issue where we might still have shared pointer references to lldb_private::Thread objects where the object itself is not valid and has been removed from the Process. When a thread is removed from a process, it will call Thread::DestroyThread() which well set a boolean member variable which is exposed now via:

bool
Thread::IsValid() const;

We then check the thread validity before handing out a shared pointer.



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@154048 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Target/ExecutionContext.cpp b/source/Target/ExecutionContext.cpp
index 3e0731d..2adfff0 100644
--- a/source/Target/ExecutionContext.cpp
+++ b/source/Target/ExecutionContext.cpp
@@ -658,13 +658,19 @@
 ExecutionContextRef::GetThreadSP () const
 {
     lldb::ThreadSP thread_sp (m_thread_wp.lock());
-    if (!thread_sp && m_tid != LLDB_INVALID_THREAD_ID)
+    if (m_tid != LLDB_INVALID_THREAD_ID)
     {
-        lldb::ProcessSP process_sp(GetProcessSP());
-        if (process_sp)
+        // We check if the thread has been destroyed in cases where clients
+        // might still have shared pointer to a thread, but the thread is
+        // not valid anymore (not part of the process)
+        if (!thread_sp || !thread_sp->IsValid())
         {
-            thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
-            m_thread_wp = thread_sp;
+            lldb::ProcessSP process_sp(GetProcessSP());
+            if (process_sp)
+            {
+                thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
+                m_thread_wp = thread_sp;
+            }
         }
     }
     return thread_sp;