I am making a shell using linux (WSL).
For some Unkown reason, when I pushback a struct pointer (that I made) into a vector it calls 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::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
Which will be the cmd_line
This is the part from std::vector that causes the segfault
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 ** **
See Question&Answers more detail:
os