[lldb-dev] lock() fails on my lldb::Type

Greg Clayton via lldb-dev lldb-dev at lists.llvm.org
Mon Jul 11 10:59:37 PDT 2016


When a class inherits from std::enable_shared_from_this<T>, like Type does:


class Type :
    public std::enable_shared_from_this<Type>,
    public UserID
{


You must construct your object directly into a shared pointer right away so that it can setup the std::weak_ptr (a member variable in the std::enable_shared_from_this class) inside of Type correctly. So your code must be:

TypeSP  p_type(new Type(LLDB_INVALID_UID,
                        NULL,
                        name,
                        1024, // just ... 
                        NULL,
                        LLDB_INVALID_UID,
                        Type::eEncodingIsUID,
                        whatever,
                        compiler_type,
                        Type::eResolveStateFull));

Don't make a pointer first, then put that pointer into a shared pointer.

Greg Clayton


> On Jul 9, 2016, at 11:10 AM, Nat! via lldb-dev <lldb-dev at lists.llvm.org> wrote:
> 
> From the looks of it, lldb uses `ObjCLanguageRuntime::LookupInCompleteClassCache` to create a lldb::Type for an Objective C class, by looking through the modules. In my circumstances (one being on Linux), it doesn't find a symbol for the class and puts my classname into `m_negative_complete_class_cache`. Which is no good for me.
> 
> I try to fix this by looking through my DeclVendor, check if a class is defined in the runtime, and if yes I create a substitute lldb::Type.
> 
> ``
>       DeclVendor *vendor = GetDeclVendor();
>       if( vendor)
>       {
>         std::vector<clang::NamedDecl*> decls;
>         uint32_t  count = vendor->FindDecls( name, false, 1, decls);
> 
>         if( count)
>         {
>             if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>( decls[ 0]))
>             {
>                CompilerType compiler_type = ClangASTContext::GetTypeForDecl(interface_decl);
>                Declaration whatever;
>                Type   *type = new Type(  LLDB_INVALID_UID,
>                                          NULL,
>                                          name,
>                                          1024, // just ... 
>                                          NULL,
>                                          LLDB_INVALID_UID,
>                                          Type::eEncodingIsUID,
>                                          whatever,
>                                          compiler_type,
>                                          Type::eResolveStateFull);
>                 TypeSP  p_type( type);
> 
>                 p_type->SetIsCompleteObjCClass( true);
>                 m_complete_class_cache[ name] = p_type;
> 
>                 return( p_type);
>              }
>           }
>       }
>    }
>    m_negative_complete_class_cache.insert(name);
>    return TypeSP();
> }
> ```
> 
> This doesn't fail outright, but the next time LookupInCompleteClassCache is called, the codes does this `lock()` with my cached type and fails:
> 
> ```
>       TypeSP complete_type_sp (complete_class_iter->second.lock());
> 
>        if (complete_type_sp)
>            return complete_type_sp;
> ```
> 
> Then the code will create a duplicate type. So what do I have to do, so that my lldb::Type doesn't return NULL when lock() is called ?
> 
> Nat!
> 
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev



More information about the lldb-dev mailing list