[cfe-dev] invalid free for CompilerInstance
Brian Cain via cfe-dev
cfe-dev at lists.llvm.org
Sat Nov 24 21:34:44 PST 2018
The message you see is a diagnostic message from your C library warning you
that the value passed in to free() (via this "delete CI" statement) is not
a pointer to something that was allocated by malloc() (via new or
otherwise).
A common cause for this kind of problem is some kind of heap or stack
corruption. Best to build your code with many warnings enabled and
consider also using ASan (Address Sanitizer). It's great at quickly
identifying the root cause of problems like this. If warnings and ASan
don't quickly identify the issue, decrease the scope [delete/disable code]
until you find the critical scope necessary to cause the problem and you
will at least have a smaller area to search for bugs.
Also, as an aside (it won't help fix the problem that you reported) --
consider using a smart pointer like std::unique_ptr for something like this
"CI" variable. For example, in this code, 'CI' leaks if the call to
InitializeSourceManager() fails.
On Sat, Nov 24, 2018 at 11:15 PM Kihong Heo via cfe-dev <
cfe-dev at lists.llvm.org> wrote:
> Hi all,
>
> I have the following code. The process works well but finally crashes at
> the last minute (delete CI) with "free(): invalid pointer”.
>
> I appreciate any help you can give me.
>
> bool run(std::string InputFileName, Action *R) {
> CI = new clang::CompilerInstance();
> assert(CI);
>
> CI->createDiagnostics();
> clang::TargetOptions &TO = CI->getTargetOpts();
> TO.Triple = llvm::sys::getDefaultTargetTriple();
> clang::TargetInfo *Target = clang::TargetInfo::CreateTargetInfo(
> CI->getDiagnostics(), CI->getInvocation().TargetOpts);
> CI->setTarget(Target);
>
> CI->createFileManager();
> CI->createSourceManager(CI->getFileManager());
> CI->createPreprocessor(clang::TU_Complete);
> CI->createASTContext();
> CI->setASTConsumer(std::unique_ptr<clang::ASTConsumer>(R));
> clang::Preprocessor &PP = CI->getPreprocessor();
> PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
> PP.getLangOpts());
>
> if (!CI->InitializeSourceManager(
> clang::FrontendInputFile(InputFileName, clang::InputKind::C))) {
> return false;
> }
>
> CI->createSema(clang::TU_Complete, 0);
> clang::DiagnosticsEngine &Diag = CI->getDiagnostics();
> Diag.setSuppressAllDiagnostics(true);
> Diag.setIgnoreAllWarnings(true);
>
> ParseAST(CI->getSema());
>
> CI->getDiagnosticClient().EndSourceFile();
>
> delete CI; // abort
> return true;
> }
>
> Thanks,
> Kihong
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
--
-Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181124/86d15526/attachment.html>
More information about the cfe-dev
mailing list