<div dir="ltr">Hi Aleksei,<div><br></div><div>I tried your below suggestion.</div><div><br></div><div><span style="font-size:12.8px">std::unique_ptr<</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">FrontendActionFactory> Factory =</span><br style="font-size:12.8px"><span style="font-size:12.8px">      newFrontendActionFactory(&</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">funcHelper.MatchFinder);</span><br style="font-size:12.8px"><span style="font-size:12.8px">Tool.run(Factory.get());</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">unfortunately, result is same (still, exception is generated).</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">It is also a good idea to check that your vector is non-empty before getting an element. -> <b>Implemented.</b></span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Let me know if you have any other suggestion. </span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">thank you very much.!!</span></div><div><br></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 28, 2016 at 12:06 AM, Aleksei Sidorin <span dir="ltr"><<a href="mailto:a.sidorin@samsung.com" target="_blank">a.sidorin@samsung.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div class="m_-95797757030923449moz-cite-prefix">> <span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"></span>Tool.run(<wbr>newFrontendActionFactory(&(<wbr>funcHelper.MatchFinder)).get()<wbr>);<br>
      <br>
      As I can guess, newFrontendActionFactory() returns std::unique_ptr
      which is destructed immediately after this statement is executed.
      So, it seems like you have a memory use-after-free. Try this
      instead:<br>
      <br>
      std::unique_ptr<<wbr>FrontendActionFactory> Factory =<br>
            newFrontendActionFactory(&<wbr>funcHelper.MatchFinder);<br>
      Tool.run(Factory.get());<br>
      <br>
      Does it help?<br>
      <br>
      It is also a good idea to check that your vector is non-empty
      before getting an element.<br>
      <br>
      <br>
      <br>
      27.12.2016 17:57, Apoorva Paneliya via cfe-dev пишет:<br>
    </div>
    <blockquote type="cite"><div><div class="h5">
      <div dir="ltr">Hi there,
        <div><br>
        </div>
        <div>Just now started with Clang. so, this might silly question.</div>
        <div><br>
        </div>
        <div>I am getting exception while calling <b>FunctionDecl-><wbr>getNameAsString()
            or  </b><b>FunctionDecl-></b><b>getName().str()</b><wbr>; </div>
        <div><br>
        </div>
        <div>Exception Details:-</div>
        <div><br>
        </div>
        <div>Type:-  "Exception thrown: read access violation."</div>
        <div>
          <div><br>
          </div>
          <div>Points to:- </div>
          <div><br>
          </div>
          <div>File:- include\clang\AST\<wbr>DeclarationNames.h</div>
          <div><br>
          </div>
          <div><b>StoredNameKind getStoredNameKind() const {</b></div>
          <div><b>    return static_cast<StoredNameKind>(<wbr>Ptr &
              PtrMask);</b></div>
          <div><b>  }</b></div>
        </div>
        <div><br>
        </div>
        <div>Actual Code </div>
        <div><br>
        </div>
        <div><b>Main.cpp</b></div>
        <div><br>
        </div>
        <div>static llvm::cl::OptionCategory
          ToolingSampleCategory("Tooling Sample");<br>
        </div>
        <div>
          <div><br>
          </div>
          <div>int main(int argc, const char **argv)</div>
          <div>{</div>
          <div><br>
          </div>
          <div>
            <div>        CommonOptionsParser OptionsParser(argc, argv,
              ToolingSampleCategory);</div>
            <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>ClangTool
              Tool(OptionsParser.<wbr>getCompilations(),</div>
            <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>OptionsParser.<wbr>getSourcePathList());</div>
          </div>
        </div>
        <div> <br>
        </div>
        <div>        DeclarationMatcher funcMatcher = functionDecl(
          ).bind("functionMatcher");</div>
        <div><br>
        </div>
        <div>
          <div>        <b>FunctionHelper funcHelper;</b></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>funcHelper.setNodeBindName("<wbr>functionMatcher");</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>funcHelper.MatchFinder.<wbr>addMatcher(funcMatcher,
            &funcHelper);</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>Tool.run(<wbr>newFrontendActionFactory(&(<wbr>funcHelper.MatchFinder)).get()<wbr>);</div>
          <div><br>
          </div>
          <div> <span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">   </span></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>llvm::SmallVector<const
            clang::FunctionDecl*, 5U> allFuncDecl =
            funcHelper.getAllFunctionDecl(<wbr>);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>const
            FunctionDecl* fnDecl = allFuncDecl[0];</div>
          <div><b><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>cout
              << fnDecl->getName().str();  /* <font color="#ff0000">Exception Generated while calling
                method.</font>  If same method called from run method
              then it works. */</b></div>
        </div>
        <div><b><br>
          </b></div>
        <div><b>       </b>return 0;</div>
        <div>}</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><b>FunctionalHelper.h</b></div>
        <div><b><br>
          </b></div>
        <div><b><br>
          </b></div>
        <div>
          <div>                typedef struct _FunctionInfo</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>std::string<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>funcName;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>std::string<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>sourceFileName;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>unsigned
            int<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>paramCount;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span>}FunctionInfo;</div>
          <div><br>
          </div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span><b>class
              FunctionHelper : public
              clang::ast_matchers::<wbr>MatchFinder::MatchCallback</b> {</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>std::vector<FunctionInfo><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                                              </span>m_vec_FunctionInfo;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>std::string<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                                                                          </span>m_str_NodeBindName;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>llvm::SmallVector<const
            clang::FunctionDecl*,5><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                    </span>m_vec_FunctionDecl;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span>private:</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>void
            run(const clang::ast_matchers::<wbr>MatchFinder::MatchResult
            &Result) override</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>const
            clang::FunctionDecl* tempFuncDecl =
Result.Nodes.getNodeAs<clang::<wbr>FunctionDecl>(m_str_<wbr>NodeBindName.c_str());</div>
          <div>                                 </div>
          <div><b>                                 //
              tempFuncDecl ->getName().str()<wbr>;  <font color="#38761d">//from
                here, it's works.</font></b></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>if
            (tempFuncDecl)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                     </span>FunctionInfo
            tempFuncInfo;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                     </span>fillFunctionInfo(tempFuncDecl,
            tempFuncInfo);</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                     </span>m_vec_FunctionInfo.push_back(<wbr>tempFuncInfo);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                                     </span>m_vec_FunctionDecl.push_back(<wbr>tempFuncDecl);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>}</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span>public:</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>clang::ast_matchers::<wbr>MatchFinder<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>MatchFinder;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>void
            setNodeBindName(std::string nodeBindName) {
            m_str_NodeBindName = nodeBindName; };</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>std::vector<FunctionInfo>
            getFuncInfoForAllMatchedNodes(<wbr>)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{<span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">    </span></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>return
            m_vec_FunctionInfo;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span></div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>llvm::SmallVector<const
            clang::FunctionDecl*,5> getAllFunctionDecl()</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>return
            m_vec_FunctionDecl;</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>static
            std::string getFunctionName(const clang::FunctionDecl*
            funcDecl)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>if
            (!funcDecl) { return ""; }</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>return
            funcDecl->getNameAsString();</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>static
            std::string getSourceFileName(const clang::FunctionDecl*
            funcDecl)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>if
            (!funcDecl) { return ""; }</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>return
funcDecl->getASTContext().<wbr>getSourceManager().<wbr>getFilename(funcDecl-><wbr>getLocation()).str();</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>static
            unsigned int getParamsCount(const clang::FunctionDecl*
            funcDecl)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>if
            (!funcDecl) { return 0; }</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>return
            funcDecl->getNumParams();</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><br>
          </div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>static
            void fillFunctionInfo(const clang::FunctionDecl* funcDecl,
            FunctionInfo &funcInfo)</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>{</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>if
            (!funcDecl) { return; }</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>funcInfo.funcName
            = FunctionHelper::<wbr>getFunctionName(funcDecl);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>funcInfo.sourceFileName
            = FunctionHelper::<wbr>getSourceFileName(funcDecl);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                             </span>funcInfo.paramCount
            = FunctionHelper::<wbr>getParamsCount(funcDecl);</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">                     </span>}</div>
          <div><span class="m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap">             </span>};</div>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>Config:- 32bit, Debug</div>
        <div>OS:- Windows</div>
        <div>Compiled through VS2015.</div>
        <div>Clang Version:- latest available. [ I guess 4.0]</div>
        <div><br>
        </div>
        <div>So, Need help in resolving issue.</div>
        <div>Thanks In Advance.!!</div>
        <div><br>
        </div>
        <div>Regards,</div>
        <div>Apoorva</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <br>
      <fieldset class="m_-95797757030923449mimeAttachmentHeader"></fieldset>
      <br>
      </div></div><pre>______________________________<wbr>_________________
cfe-dev mailing list
<a class="m_-95797757030923449moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>
<a class="m_-95797757030923449moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><span class="HOEnZb"><font color="#888888">
</font></span></pre><span class="HOEnZb"><font color="#888888">
    </font></span></blockquote><span class="HOEnZb"><font color="#888888">
    <br>
    <p><br>
    </p>
    <pre class="m_-95797757030923449moz-signature" cols="72">-- 
Best regards,
Aleksei Sidorin
Software Engineer, 
IMSWL-IMCG, SRR, Samsung Electronics
</pre>
  </font></span></div>

</blockquote></div><br></div>