Fix up how the ValueObjects manage their life cycle so that you can hand out a shared
pointer to a ValueObject or any of its dependent ValueObjects, and the whole cluster will
stay around as long as that shared pointer stays around.

llvm-svn: 130035
diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
index d95806c..2b1beed 100644
--- a/lldb/source/Core/ValueObject.cpp
+++ b/lldb/source/Core/ValueObject.cpp
@@ -59,9 +59,11 @@
     m_location_str (),
     m_summary_str (),
     m_object_desc_str (),
+    m_manager(parent.GetManager()),
     m_children (),
     m_synthetic_children (),
-    m_dynamic_value_sp (),
+    m_dynamic_value (NULL),
+    m_deref_valobj(NULL),
     m_format (eFormatDefault),
     m_value_is_valid (false),
     m_value_did_change (false),
@@ -70,6 +72,7 @@
     m_pointers_point_to_load_addrs (false),
     m_is_deref_of_parent (false)
 {
+    m_manager->ManageObject(this);
 }
 
 //----------------------------------------------------------------------
@@ -88,9 +91,11 @@
     m_location_str (),
     m_summary_str (),
     m_object_desc_str (),
+    m_manager(),
     m_children (),
     m_synthetic_children (),
-    m_dynamic_value_sp (),
+    m_dynamic_value (NULL),
+    m_deref_valobj(NULL),
     m_format (eFormatDefault),
     m_value_is_valid (false),
     m_value_did_change (false),
@@ -99,6 +104,8 @@
     m_pointers_point_to_load_addrs (false),
     m_is_deref_of_parent (false)
 {
+    m_manager = new ValueObjectManager();
+    m_manager->ManageObject (this);
 }
 
 //----------------------------------------------------------------------
@@ -282,14 +289,15 @@
         if (idx < GetNumChildren())
         {
             // Check if we have already made the child value object?
-            if (can_create && m_children[idx].get() == NULL)
+            if (can_create && m_children[idx] == NULL)
             {
                 // No we haven't created the child at this index, so lets have our
                 // subclass do it and cache the result for quick future access.
                 m_children[idx] = CreateChildAtIndex (idx, false, 0);
             }
-
-            child_sp = m_children[idx];
+            
+            if (m_children[idx] != NULL)
+                return m_children[idx]->GetSP();
         }
     }
     return child_sp;
@@ -377,10 +385,10 @@
     m_name = name;
 }
 
-ValueObjectSP
+ValueObject *
 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
 {
-    ValueObjectSP valobj_sp;
+    ValueObject *valobj;
     
     if (UpdateValueIfNeeded())
     {
@@ -420,22 +428,22 @@
             if (!child_name_str.empty())
                 child_name.SetCString (child_name_str.c_str());
 
-            valobj_sp.reset (new ValueObjectChild (*this,
-                                                   clang_ast,
-                                                   child_clang_type,
-                                                   child_name,
-                                                   child_byte_size,
-                                                   child_byte_offset,
-                                                   child_bitfield_bit_size,
-                                                   child_bitfield_bit_offset,
-                                                   child_is_base_class,
-                                                   child_is_deref_of_parent));
+            valobj = new ValueObjectChild (*this,
+                                           clang_ast,
+                                           child_clang_type,
+                                           child_name,
+                                           child_byte_size,
+                                           child_byte_offset,
+                                           child_bitfield_bit_size,
+                                           child_bitfield_bit_offset,
+                                           child_is_base_class,
+                                           child_is_deref_of_parent);            
             if (m_pointers_point_to_load_addrs)
-                valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
+                valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
         }
     }
     
-    return valobj_sp;
+    return valobj;
 }
 
 const char *
@@ -913,18 +921,18 @@
 }
 
 void
-ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
+ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
 {
-    m_synthetic_children[key] = valobj_sp;
+    m_synthetic_children[key] = valobj;
 }
 
 ValueObjectSP
 ValueObject::GetSyntheticChild (const ConstString &key) const
 {
     ValueObjectSP synthetic_child_sp;
-    std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
+    std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
     if (pos != m_synthetic_children.end())
-        synthetic_child_sp = pos->second;
+        synthetic_child_sp = pos->second->GetSP();
     return synthetic_child_sp;
 }
 
@@ -960,13 +968,17 @@
         synthetic_child_sp = GetSyntheticChild (index_const_str);
         if (!synthetic_child_sp)
         {
+            ValueObject *synthetic_child;
             // We haven't made a synthetic array member for INDEX yet, so
             // lets make one and cache it for any future reference.
-            synthetic_child_sp = CreateChildAtIndex(0, true, index);
+            synthetic_child = CreateChildAtIndex(0, true, index);
 
             // Cache the value if we got one back...
-            if (synthetic_child_sp)
-                AddSyntheticChild(index_const_str, synthetic_child_sp);
+            if (synthetic_child)
+            {
+                AddSyntheticChild(index_const_str, synthetic_child);
+                synthetic_child_sp = synthetic_child->GetSP();
+            }
         }
     }
     return synthetic_child_sp;
@@ -975,7 +987,7 @@
 void
 ValueObject::CalculateDynamicValue ()
 {
-    if (!m_dynamic_value_sp && !IsDynamic())
+    if (!m_dynamic_value && !IsDynamic())
     {
         Process *process = m_update_point.GetProcess();
         bool worth_having_dynamic_value = false;
@@ -1005,33 +1017,25 @@
         }
         
         if (worth_having_dynamic_value)
-            m_dynamic_value_sp.reset (new ValueObjectDynamicValue (*this));
+            m_dynamic_value = new ValueObjectDynamicValue (*this);
+            
+//        if (worth_having_dynamic_value)
+//            printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
+
     }
 }
 
-lldb::ValueObjectSP
+ValueObjectSP
 ValueObject::GetDynamicValue (bool can_create)
 {
-    if (!IsDynamic() && m_dynamic_value_sp == NULL && can_create)
+    if (!IsDynamic() && m_dynamic_value == NULL && can_create)
     {
         CalculateDynamicValue();
     }
-    return m_dynamic_value_sp;
-}
-
-lldb::ValueObjectSP
-ValueObject::GetDynamicValue (bool can_create, lldb::ValueObjectSP &owning_valobj_sp)
-{
-    if (!IsDynamic() && m_dynamic_value_sp == NULL && can_create)
-    {
-        CalculateDynamicValue();
-        if (m_dynamic_value_sp)
-        {
-            ValueObjectDynamicValue *as_dynamic_value = static_cast<ValueObjectDynamicValue *>(m_dynamic_value_sp.get());
-            as_dynamic_value->SetOwningSP (owning_valobj_sp);
-        }
-    }
-    return m_dynamic_value_sp;
+    if (m_dynamic_value)
+        return m_dynamic_value->GetSP();
+    else
+        return ValueObjectSP();
 }
 
 bool
@@ -1352,17 +1356,17 @@
 
             m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
 
-            valobj_sp.reset (new ValueObjectConstResult (exe_scope, 
-                                                         ast,
-                                                         GetClangType(),
-                                                         name,
-                                                         data));
+            valobj_sp = ValueObjectConstResult::Create (exe_scope, 
+                                                        ast,
+                                                        GetClangType(),
+                                                        name,
+                                                        data);
         }
     }
     
     if (!valobj_sp)
     {
-        valobj_sp.reset (new ValueObjectConstResult (NULL, m_error));
+        valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
     }
     return valobj_sp;
 }
@@ -1370,8 +1374,8 @@
 lldb::ValueObjectSP
 ValueObject::Dereference (Error &error)
 {
-    if (m_deref_valobj_sp)
-        return m_deref_valobj_sp;
+    if (m_deref_valobj)
+        return m_deref_valobj->GetSP();
         
     const bool is_pointer_type = IsPointerType();
     if (is_pointer_type)
@@ -1408,22 +1412,23 @@
             if (!child_name_str.empty())
                 child_name.SetCString (child_name_str.c_str());
 
-            m_deref_valobj_sp.reset (new ValueObjectChild (*this,
-                                                           clang_ast,
-                                                           child_clang_type,
-                                                           child_name,
-                                                           child_byte_size,
-                                                           child_byte_offset,
-                                                           child_bitfield_bit_size,
-                                                           child_bitfield_bit_offset,
-                                                           child_is_base_class,
-                                                           child_is_deref_of_parent));
+            m_deref_valobj = new ValueObjectChild (*this,
+                                                   clang_ast,
+                                                   child_clang_type,
+                                                   child_name,
+                                                   child_byte_size,
+                                                   child_byte_offset,
+                                                   child_bitfield_bit_size,
+                                                   child_bitfield_bit_offset,
+                                                   child_is_base_class,
+                                                   child_is_deref_of_parent);
         }
     }
 
-    if (m_deref_valobj_sp)
+    if (m_deref_valobj)
     {
         error.Clear();
+        return m_deref_valobj->GetSP();
     }
     else
     {
@@ -1434,9 +1439,8 @@
             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
         else
             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
+        return ValueObjectSP();
     }
-
-    return m_deref_valobj_sp;
 }
 
 lldb::ValueObjectSP
@@ -1472,13 +1476,13 @@
                 {
                     std::string name (1, '&');
                     name.append (m_name.AsCString(""));
-                    m_addr_of_valobj_sp.reset (new ValueObjectConstResult (GetExecutionContextScope(),
-                                                                           ast, 
-                                                                           ClangASTContext::CreatePointerType (ast, clang_type),
-                                                                           ConstString (name.c_str()),
-                                                                           addr, 
-                                                                           eAddressTypeInvalid,
-                                                                           m_data.GetAddressByteSize()));
+                    m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
+                                                                          ast, 
+                                                                          ClangASTContext::CreatePointerType (ast, clang_type),
+                                                                          ConstString (name.c_str()),
+                                                                          addr, 
+                                                                          eAddressTypeInvalid,
+                                                                          m_data.GetAddressByteSize());
                 }
             }
             break;
diff --git a/lldb/source/Core/ValueObjectConstResult.cpp b/lldb/source/Core/ValueObjectConstResult.cpp
index ff5d265..fbb35eb 100644
--- a/lldb/source/Core/ValueObjectConstResult.cpp
+++ b/lldb/source/Core/ValueObjectConstResult.cpp
@@ -26,6 +26,19 @@
 using namespace lldb;
 using namespace lldb_private;
 
+ValueObjectSP
+ValueObjectConstResult::Create
+(
+    ExecutionContextScope *exe_scope,
+    ByteOrder byte_order, 
+    uint32_t addr_byte_size
+)
+{
+    return (new ValueObjectConstResult (exe_scope,
+                                        byte_order,
+                                        addr_byte_size))->GetSP();
+}
+
 ValueObjectConstResult::ValueObjectConstResult
 (
     ExecutionContextScope *exe_scope,
@@ -44,6 +57,23 @@
     m_pointers_point_to_load_addrs = true;
 }
 
+ValueObjectSP
+ValueObjectConstResult::Create
+(
+    ExecutionContextScope *exe_scope,
+    clang::ASTContext *clang_ast,
+    void *clang_type,
+    const ConstString &name,
+    const DataExtractor &data
+)
+{
+    return (new ValueObjectConstResult (exe_scope,
+                                        clang_ast,
+                                        clang_type,
+                                        name,
+                                        data))->GetSP();
+}
+
 ValueObjectConstResult::ValueObjectConstResult
 (
     ExecutionContextScope *exe_scope,
@@ -67,6 +97,27 @@
     m_pointers_point_to_load_addrs = true;
 }
 
+ValueObjectSP
+ValueObjectConstResult::Create
+(
+    ExecutionContextScope *exe_scope,
+    clang::ASTContext *clang_ast,
+    void *clang_type,
+    const ConstString &name,
+    const lldb::DataBufferSP &data_sp,
+    lldb::ByteOrder data_byte_order, 
+    uint8_t data_addr_size
+)
+{
+    return (new ValueObjectConstResult (exe_scope,
+                                        clang_ast,
+                                        clang_type,
+                                        name,
+                                        data_sp,
+                                        data_byte_order,
+                                        data_addr_size))->GetSP();
+}
+
 ValueObjectConstResult::ValueObjectConstResult
 (
     ExecutionContextScope *exe_scope,
@@ -94,6 +145,27 @@
     m_pointers_point_to_load_addrs = true;
 }
 
+ValueObjectSP
+ValueObjectConstResult::Create
+(
+    ExecutionContextScope *exe_scope,
+    clang::ASTContext *clang_ast,
+    void *clang_type,
+    const ConstString &name,
+    lldb::addr_t address,
+    AddressType address_type,
+    uint8_t addr_byte_size
+)
+{
+    return (new ValueObjectConstResult (exe_scope,
+                                        clang_ast,
+                                        clang_type,
+                                        name,
+                                        address,
+                                        address_type,
+                                        addr_byte_size))->GetSP();
+}
+
 ValueObjectConstResult::ValueObjectConstResult 
 (
     ExecutionContextScope *exe_scope,
@@ -128,6 +200,17 @@
     m_pointers_point_to_load_addrs = true;
 }
 
+ValueObjectSP
+ValueObjectConstResult::Create
+(
+    ExecutionContextScope *exe_scope,
+    const Error& error
+)
+{
+    return (new ValueObjectConstResult (exe_scope,
+                                        error))->GetSP();
+}
+
 ValueObjectConstResult::ValueObjectConstResult (
     ExecutionContextScope *exe_scope,
     const Error& error) :
diff --git a/lldb/source/Core/ValueObjectDynamicValue.cpp b/lldb/source/Core/ValueObjectDynamicValue.cpp
index 2b80ae5..17eb90a 100644
--- a/lldb/source/Core/ValueObjectDynamicValue.cpp
+++ b/lldb/source/Core/ValueObjectDynamicValue.cpp
@@ -39,14 +39,6 @@
     m_address (),
     m_type_sp()
 {
-    // THINK ABOUT: It looks ugly to doctor up the name like this.  But if
-    // people find it confusing to tell the difference, we may want to do something...
-    
-//    std::string dynamic_name ("<dynamic value for \"");
-//    dynamic_name.append(parent.GetName().AsCString());
-//    dynamic_name.append("\">");
-//    
-//    SetName (dynamic_name.c_str()); 
     SetName (parent.GetName().AsCString());
 }
 
diff --git a/lldb/source/Core/ValueObjectMemory.cpp b/lldb/source/Core/ValueObjectMemory.cpp
index d399848..aa9ff0a 100644
--- a/lldb/source/Core/ValueObjectMemory.cpp
+++ b/lldb/source/Core/ValueObjectMemory.cpp
@@ -30,9 +30,18 @@
 #include "lldb/Target/Target.h"
 #include "lldb/Target/Thread.h"
 
-
+using namespace lldb;
 using namespace lldb_private;
 
+ValueObjectSP
+ValueObjectMemory::Create (ExecutionContextScope *exe_scope, 
+                           const char *name,
+                           const Address &address, 
+                           lldb::TypeSP &type_sp)
+{
+    return (new ValueObjectMemory (exe_scope, name, address, type_sp))->GetSP();
+}
+
 ValueObjectMemory::ValueObjectMemory (ExecutionContextScope *exe_scope,
                                       const char *name, 
                                       const Address &address,
diff --git a/lldb/source/Core/ValueObjectRegister.cpp b/lldb/source/Core/ValueObjectRegister.cpp
index 979c28c9..df80ce3 100644
--- a/lldb/source/Core/ValueObjectRegister.cpp
+++ b/lldb/source/Core/ValueObjectRegister.cpp
@@ -95,21 +95,29 @@
     return m_error.Success();
 }
 
-ValueObjectSP
+ValueObject *
 ValueObjectRegisterContext::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
 {
-    ValueObjectSP valobj_sp;
-
+    ValueObject *new_valobj = NULL;
+    
     const uint32_t num_children = GetNumChildren();
     if (idx < num_children)
-        valobj_sp.reset (new ValueObjectRegisterSet(GetExecutionContextScope(), m_reg_ctx_sp, idx));
-    return valobj_sp;
+        new_valobj = new ValueObjectRegisterSet(GetExecutionContextScope(), m_reg_ctx_sp, idx);
+    
+    return new_valobj;
 }
 
 
 #pragma mark -
 #pragma mark ValueObjectRegisterSet
 
+ValueObjectSP
+ValueObjectRegisterSet::Create (ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, uint32_t set_idx)
+{
+    return (new ValueObjectRegisterSet (exe_scope, reg_ctx_sp, set_idx))->GetSP();
+}
+
+
 ValueObjectRegisterSet::ValueObjectRegisterSet (ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx, uint32_t reg_set_idx) :
     ValueObject (exe_scope),
     m_reg_ctx_sp (reg_ctx),
@@ -199,30 +207,33 @@
 }
 
 
-ValueObjectSP
+ValueObject *
 ValueObjectRegisterSet::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
 {
-    ValueObjectSP valobj_sp;
+    ValueObject *valobj;
     if (m_reg_ctx_sp && m_reg_set)
     {
         const uint32_t num_children = GetNumChildren();
         if (idx < num_children)
-            valobj_sp.reset (new ValueObjectRegister(*this, m_reg_ctx_sp, m_reg_set->registers[idx]));
+            valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, m_reg_set->registers[idx]);
     }
-    return valobj_sp;
+    return valobj;
 }
 
 lldb::ValueObjectSP
 ValueObjectRegisterSet::GetChildMemberWithName (const ConstString &name, bool can_create)
 {
-    ValueObjectSP valobj_sp;
+    ValueObject *valobj = NULL;
     if (m_reg_ctx_sp && m_reg_set)
     {
         const RegisterInfo *reg_info = m_reg_ctx_sp->GetRegisterInfoByName (name.AsCString());
         if (reg_info != NULL)
-            valobj_sp.reset (new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info->kinds[eRegisterKindLLDB]));
+            valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info->kinds[eRegisterKindLLDB]);
     }
-    return valobj_sp;
+    if (valobj)
+        return valobj->GetSP();
+    else
+        return ValueObjectSP();
 }
 
 uint32_t
@@ -265,6 +276,12 @@
     ConstructObject();
 }
 
+ValueObjectSP
+ValueObjectRegister::Create (ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, uint32_t reg_num)
+{
+    return (new ValueObjectRegister (exe_scope, reg_ctx_sp, reg_num))->GetSP();
+}
+
 ValueObjectRegister::ValueObjectRegister (ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx, uint32_t reg_num) :
     ValueObject (exe_scope),
     m_reg_ctx_sp (reg_ctx),
diff --git a/lldb/source/Core/ValueObjectVariable.cpp b/lldb/source/Core/ValueObjectVariable.cpp
index 7bab591..882b702 100644
--- a/lldb/source/Core/ValueObjectVariable.cpp
+++ b/lldb/source/Core/ValueObjectVariable.cpp
@@ -32,6 +32,12 @@
 
 using namespace lldb_private;
 
+lldb::ValueObjectSP
+ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
+{
+    return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
+}
+
 ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
     ValueObject(exe_scope),
     m_variable_sp(var_sp)