[英]SIGNALSEGV (segmentation fault) out of vector pushback method
I am making a shell using linux (WSL).我正在使用 linux (WSL) 制作 shell。 For some Unkown reason, when I pushback a struct pointer (that I made) into a vector it calls SIGNALSEGV.由于某些未知的原因,当我将一个结构指针(我制作的)推回一个向量时,它调用了 SIGNALSEGV。
These are the main classes of the problematic code,这些是有问题的代码的主要类,
class TimeoutCommand : public BuiltInCommand {
public:
bool background;
std::string line;
int pid;
int duration;
TimeoutCommand(const char* cmd_line); //prepare line
virtual ~TimeoutCommand() {}
void execute() override; ////set alarm + fork + add to joblist + add to timelist
};
class TimeoutList{
public:
struct TimeoutEntry {
std::string line;
int pid;
time_t start_time;
int duration;
TimeoutEntry(int pid,time_t start_time,std::string line,int duration)
:pid(pid),start_time(start_time),line(line),duration(duration)
{};
};
std::vector<TimeoutEntry*> TimeoutVec;
TimeoutList();
~TimeoutList() {
for (TimeoutEntry *entry : TimeoutVec)
delete entry;
}
void addCommand(TimeoutCommand* cmd); ////add new timeout
void timeoutCheck(); ////timout timedoutcommands
};
This is TimeoutCommand constructor and the line that calls the problematic function:这是 TimeoutCommand 构造函数和调用有问题的 function 的行:
TimeoutCommand::TimeoutCommand(const char *cmd_line)
:BuiltInCommand(cmd_line)
{
_parseCommandLine(cmd_line,args);
background=_isBackgroundComamnd(cmd_line);
for(int i=2;args[i]!=NULL;i++) {
line +=args[i];
}
duration=stoi(args[1]);
}
void TimeoutCommand::execute() {
alarm(duration);
**SmallShell::getInstance().timouts->addCommand(this);**
...
...
...
And finally here is the problematic fucntion:最后是有问题的功能:
void TimeoutList::addCommand(TimeoutCommand *cmd) {
time_t t= time(NULL);
if (t==-1){
perror("smash error: time failed");
return;
}
TimeoutEntry* entry = new TimeoutEntry(cmd->pid,t,cmd->line,cmd->duration);
**TimeoutVec.push_back(entry);**
} }
What is causing the segmentation fault?是什么导致分段错误? I dont see any wierd pointer messups or anything like that right away.我没有立即看到任何奇怪的指针混乱或类似的东西。
running this command woul look something like: timeout 3 sleep 10运行这个命令看起来像: timeout 3 sleep 10
Which will be the cmd_line这将是 cmd_line
This is the part from std::vector that causes the segfault这是 std::vector 中导致段错误的部分
push_back(const value_type& __x)
{
**if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)**
{
_GLIBCXX_ASAN_ANNOTATE_GROW(1);
_Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
__x);
++this->_M_impl._M_finish;
_GLIBCXX_ASAN_ANNOTATE_GREW(1);
}
else
_M_realloc_insert(end(), __x);
}
**Problematic lines are marked with ** ** **有问题的线路标有** **
The problem was that i instialized a pointer to an instance of TimeoutList without allocating a new istance.问题是我初始化了一个指向 TimeoutList 实例的指针,而没有分配一个新的 istance。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.