CompilerInstance :: setInvocation在运行时失败:"释放的指针未分配"

时间:2014-05-21 15:45:13

标签: c++ clang llvm llvm-clang

我试图使用Clang,而且我的引用计数机制存在问题。 每当我尝试将CompilerInvocation分配给CompilerInstance时,在运行时我都会收到错误:

clangjit(13823,0x7fff7646f310) malloc: *** error for object 0x103002c00: pointer being freed was not allocated

这是一个重现问题的最小程序:

#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>

using namespace clang;
int main(int argc, const char* const *argv) {
    CompilerInstance* CI = new CompilerInstance();
    CI->setInvocation(new CompilerInvocation());
}

CompilerInstance在内部使用以下方法进行引用计数:

IntrusiveRefCntPtr<CompilerInvocation> Invocation;

并且setter定义为:

void CompilerInstance::setInvocation(CompilerInvocation *Value) {
  Invocation = Value;
}

(因为他们来自Clang,我无法控制其中任何一个)。

这是故障点的堆栈跟踪:

#0  0x00007fff8d097866 in __pthread_kill ()
#1  0x00007fff8df2635c in pthread_kill ()
#2  0x00007fff8d03ab1a in abort ()
#3  0x00007fff8485807f in free ()
#4  0x0000000100002678 in std::__1::allocator<std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*> >::deallocate(std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*>*, unsigned long) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1632
#5  0x000000010000266c in std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*> > >::deallocate(std::__1::allocator<std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*> >&, std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*>*, unsigned long) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1443
#6  0x0000000100002654 in std::__1::__tree<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::destroy(std::__1::__tree_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void*>*) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__tree:1446
#7  0x0000000100002551 in ~__tree at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__tree:1433
#8  0x00000001000024f5 in ~__tree at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__tree:1432
#9  0x00000001000024d5 in ~set at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/set:398
#10 0x0000000100002325 in ~set at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/set:398
#11 0x00000001000022f5 in ~SmallSet at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/SmallSet.h:31
#12 0x00000001000022c5 in ~SmallSet at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/SmallSet.h:31
#13 0x000000010000229e in ~SetVector at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/SetVector.h:37
#14 0x0000000100001de5 in ~SetVector at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/SetVector.h:37
#15 0x0000000100001d55 in ~HeaderSearchOptions at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/clang/Lex/HeaderSearchOptions.h:45
#16 0x0000000100001d25 in ~HeaderSearchOptions at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/clang/Lex/HeaderSearchOptions.h:45
#17 0x0000000100001cf3 in llvm::RefCountedBase<clang::HeaderSearchOptions>::Release() const at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/IntrusiveRefCntPtr.h:54
#18 0x0000000100001c45 in llvm::IntrusiveRefCntPtrInfo<clang::HeaderSearchOptions>::release(clang::HeaderSearchOptions*) at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/IntrusiveRefCntPtr.h:89
#19 0x0000000100001c1d in llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>::release() at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/IntrusiveRefCntPtr.h:178
#20 0x0000000100001bd1 in ~IntrusiveRefCntPtr at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/IntrusiveRefCntPtr.h:148
#21 0x0000000100001315 in ~IntrusiveRefCntPtr at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/llvm/ADT/IntrusiveRefCntPtr.h:148
#22 0x0000000100001297 in ~CompilerInvocationBase at /Users/juancn/Downloads/clang+llvm-3.4.1-x86_64-apple-darwin10.9/include/clang/Frontend/CompilerInvocation.h:52
#23 0x0000000100254435 in clang::CompilerInstance::setInvocation(clang::CompilerInvocation*) ()
#24 0x0000000100000ea4 in main at /Users/juancn/projects/clangjit/clangjit/clangjit/main.cpp:7

我不明白为什么setInvocation()在CompilerInvocationBase上调用析构函数,这似乎是失败的原因(我的C ++有点生疏,所以我很可能错过了显而易见的事情。)

更新:我刚发现只是删除对象会导致问题:

CompilerInvocation* CI = new CompilerInvocation;
delete CI;

所以它看起来像是Clang中的一个错误。我仍然没有办法解决它。

1 个答案:

答案 0 :(得分:0)

我想通了(有点)。我正在为OSX(clang+llvm-3.4.1-x86_64-apple-darwin10.9)链接到clang二进制文件,但似乎有些东西已经关闭了。

我从头开始重建了Clang + LLVM,并使用了本地构建的库。这解决了这个问题。