Sema::CurScope not initialized ??

I’m using these codes to watch scope:

DeclContext *ctx = D->getDeclContext(); // VarDecl *D
Scope *scope = compiler.getSema().getScopeForContext(ctx); // CompilerInstance compiler
while (scope && scope->getParent()) {
scope = scope->getParent();
printf(“parent scope\n”);
}

And the program crashed at getScopeForContext. I check out the function implementation, and I found Sema::CurScope pointer has not been initialized, in my vs2010 the value is 0xcdcdcd… What’s wrong ?

Thanks