My clang program crashes during initialisation, gives glibc detected, free(): invalid pointer

Hi, I wrote a clang program which traversed an ast, a while ago on windows 7
(think it was 32bit). But now I've recently reinstalled llvm/clang from the
repository on ubuntu 12.04 64bit, with gcc/g++ 4.6.3.

And when I try run the code from my original program to initialise clang,
*cextract2.cpp:*

#include <clang/Basic/Diagnostic.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <llvm/Support/Host.h>

int main(int argc, char* argv[]) {
  clang::CompilerInstance ci;

  //
  ci.createDiagnostics(0,0);
  clang::DiagnosticsEngine &diagnostics=ci.getDiagnostics();
  diagnostics.setIgnoreAllWarnings(true);

  //
  clang::TargetOptions to;
  to.Triple = llvm::sys::getDefaultTargetTriple();

  //
  clang::TargetInfo *pti=
    clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(),to);

  ci.setTarget(pti); //crashes on this function call
  return 0;
}

*I get these errors:*

*** glibc detected *** bin/cextract2: free(): invalid pointer:
0x00007f7be917a76
0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f7be8e40b96]
bin/cextract2[0x417707]
bin/cextract2[0xa0c13c]
bin/cextract2[0xa1d709]
bin/cextract2[0x41074c]
bin/cextract2[0x40fa26]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f7be8de376d]
bin/cextract2[0x4103d9]
======= Memory map: ========
00400000-00b93000 r-xp 00000000 08:07 6293906
/home/a
ndrew/Desktop/cextract2/bin/cextract2
00d93000-00dea000 r--p 00793000 08:07 6293906
/home/a
ndrew/Desktop/cextract2/bin/cextract2
00dea000-00deb000 rw-p 007ea000 08:07 6293906
/home/a
ndrew/Desktop/cextract2/bin/cextract2
00deb000-00ded000 rw-p 00000000 00:00 0
019d1000-019f2000 rw-p 00000000 00:00 0
[heap]
7f7be8bac000-7f7be8bc1000 r-xp 00000000 08:06 1314383
/lib/x8
6_64-linux-gnu/libgcc_s.so.1
7f7be8bc1000-7f7be8dc0000 ---p 00015000 08:06 1314383
/lib/x8
6_64-linux-gnu/libgcc_s.so.1
7f7be8dc0000-7f7be8dc1000 r--p 00014000 08:06 1314383
/lib/x8
6_64-linux-gnu/libgcc_s.so.1
7f7be8dc1000-7f7be8dc2000 rw-p 00015000 08:06 1314383
/lib/x8
6_64-linux-gnu/libgcc_s.so.1
7f7be8dc2000-7f7be8f77000 r-xp 00000000 08:06 1319373
/lib/x8
6_64-linux-gnu/libc-2.15.so
7f7be8f77000-7f7be9176000 ---p 001b5000 08:06 1319373
/lib/x8
6_64-linux-gnu/libc-2.15.so
7f7be9176000-7f7be917a000 r--p 001b4000 08:06 1319373
/lib/x8
6_64-linux-gnu/libc-2.15.so
7f7be917a000-7f7be917c000 rw-p 001b8000 08:06 1319373
/lib/x8
6_64-linux-gnu/libc-2.15.so
7f7be917c000-7f7be9181000 rw-p 00000000 00:00 0
7f7be9181000-7f7be927c000 r-xp 00000000 08:06 1319381
/lib/x8
6_64-linux-gnu/libm-2.15.so
7f7be927c000-7f7be947b000 ---p 000fb000 08:06 1319381
/lib/x8
6_64-linux-gnu/libm-2.15.so
7f7be947b000-7f7be947c000 r--p 000fa000 08:06 1319381
/lib/x8
6_64-linux-gnu/libm-2.15.so
7f7be947c000-7f7be947d000 rw-p 000fb000 08:06 1319381
/lib/x8
6_64-linux-gnu/libm-2.15.so
7f7be947d000-7f7be955f000 r-xp 00000000 08:06 2105405
/usr/li
b/x86_64-linux-gnu/libstdc++.so.6.0.16
7f7be955f000-7f7be975e000 ---p 000e2000 08:06 2105405
/usr/li
b/x86_64-linux-gnu/libstdc++.so.6.0.16
7f7be975e000-7f7be9766000 r--p 000e1000 08:06 2105405
/usr/li
b/x86_64-linux-gnu/libstdc++.so.6.0.16
7f7be9766000-7f7be9768000 rw-p 000e9000 08:06 2105405
/usr/li
b/x86_64-linux-gnu/libstdc++.so.6.0.16
7f7be9768000-7f7be977d000 rw-p 00000000 00:00 0
7f7be977d000-7f7be977f000 r-xp 00000000 08:06 1319372
/lib/x8
6_64-linux-gnu/libdl-2.15.so
7f7be977f000-7f7be997f000 ---p 00002000 08:06 1319372
/lib/x8
6_64-linux-gnu/libdl-2.15.so
7f7be997f000-7f7be9980000 r--p 00002000 08:06 1319372
/lib/x8
6_64-linux-gnu/libdl-2.15.so
7f7be9980000-7f7be9981000 rw-p 00003000 08:06 1319372
/lib/x8
6_64-linux-gnu/libdl-2.15.so
7f7be9981000-7f7be9999000 r-xp 00000000 08:06 1319375
/lib/x8
6_64-linux-gnu/libpthread-2.15.so
7f7be9999000-7f7be9b98000 ---p 00018000 08:06 1319375
/lib/x8
6_64-linux-gnu/libpthread-2.15.so
7f7be9b98000-7f7be9b99000 r--p 00017000 08:06 1319375
/lib/x8
6_64-linux-gnu/libpthread-2.15.so
7f7be9b99000-7f7be9b9a000 rw-p 00018000 08:06 1319375
/lib/x8
6_64-linux-gnu/libpthread-2.15.so
7f7be9b9a000-7f7be9b9e000 rw-p 00000000 00:00 0
7f7be9b9e000-7f7be9bc0000 r-xp 00000000 08:06 1319387
/lib/x8
6_64-linux-gnu/ld-2.15.so
7f7be9d99000-7f7be9d9f000 rw-p 00000000 00:00 0
7f7be9dbd000-7f7be9dc0000 rw-p 00000000 00:00 0
7f7be9dc0000-7f7be9dc1000 r--p 00022000 08:06 1319387
/lib/x8
6_64-linux-gnu/ld-2.15.so
7f7be9dc1000-7f7be9dc3000 rw-p 00023000 08:06 1319387
/lib/x8
6_64-linux-gnu/ld-2.15.so
7ffffaac2000-7ffffaae3000 rw-p 00000000 00:00 0
[stack]
7ffffabff000-7ffffac00000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsysca
ll]
Aborted (core dumped)

*Makefile:*
EXE = $(OUT)/cextract2
OUT = bin
CC = g++

CPPFLAGS := `llvm-config --cxxflags`
LDFLAGS := \
  -lclangFrontend \
  -lclangDriver \
  -lclangSerialization \
  -lclangParse \
  -lclangSema \
  -lclangAnalysis \
  -lclangEdit \
  -lclangAST \
  -lclangLex \
  -lclangBasic \
  `llvm-config --libs` \
  `llvm-config --ldflags`

objs := $(patsubst %.cpp,$(OUT)/%.o,$(wildcard *.cpp))
deps := $(objs:.o=.dep)

.PHONY: all
all: $(EXE)

-include $(deps)

$(OUT)/%.o: %.cpp
  mkdir -p $(@D)
  $(CC) $(CPPFLAGS) -c $< -o $@
  $(CC) $(CPPFLAGS) -MM $< | sed -e '1,1 s|[^:]*:|$@:|' > $(@D)/$*.dep

$(EXE) : $(objs)
  $(CC) $^ $(LDFLAGS) -o $@

Ok I just realised the problem is when CompilerInstance frees itself when the
program ends (nothing to do set ci.setTarget).

I'm not sure why it's doing that, as a temporary measure I'm just declaring
the CompilerInstance as a pointer and not freeing it, though for aesthetics
I'd prefer not to.

I also wanted to add it was only when ci.setTarget was set that the
compilerinstance would crash upon being deleted.