错误描述:部分代码莫名重复调用导致 Segmentation fault (core dumped)
文件一:
代码: 全选
{
std::string java_path = this->jdk_path + "/bin/java" + " -version";
std::string str = utils::exec(java_path); //本调用会出现异常
//下面注释中的代码可以正常执行
// char *cmd = (char*)java_path.c_str();
// std::string str = utils::exec(cmd);
cout << str << endl;
}
代码: 全选
#ifndef UTIL_H
#define UTIL_H
#include <sys/stat.h>
namespace silly_monkey
{
namespace utils
{
//判断文件是否存在
inline bool file_exists (const std::string& name) {
struct stat buffer;
return ( stat(name.c_str(), &buffer) == 0 );
}
//执行命令并返回执行结果
inline std::string exec(std::string str_cmd)
{
cout << "00" << endl;
// char *cmd = (char*)str_cmd.data();
char *cmd = (char*)str_cmd.c_str();
cout << "11" << cmd << endl;
std::string result = utils::exec(cmd);//这里崩溃了,我们希望通过解决Segmentation fault (core dumped) 使用gdb定位崩溃原因
//还没成功。
//上述方法自始至终都没有被调用
//00 11 之间的代码被重复调用
cout << "22" << endl;
return result;
// return str_cmd;
}
//执行命令并返回执行结果
inline std::string exec(const char* cmd)
{
cout << "--------------------" << endl;
cout << cmd << endl;
cout << "--------------------" << endl;
char buffer[128];
std::string result = "";
FILE *pipe = popen(cmd, "r");
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
try
{
while ( !feof(pipe) )
{
if ( fgets(buffer, sizeof(buffer), pipe) != NULL )
result += buffer;
}
}
catch (...)
{
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
}
}
#endif
崩溃打印信息:
代码: 全选
00
11/beyourself/DevTool/DevKit/Java/jdk1.7.0_80/bin/java -version
//上面两行大量重复出现
Segmentation fault (core dumped)
代码: 全选
warning: core file may not match specified executable file.
[New LWP 4506]
Core was generated by `./test_self_check.run'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007f37069ccbbc in _int_malloc (av=av@entry=0x7f3706d0fb20 <main_arena>, bytes=bytes@entry=62) at malloc.c:3353
3353 malloc.c: No such file or directory.