Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

c++ - SIGNALSEGV (segmentation fault) out of vector pushback method

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem was that i instialized a pointer to an instance of TimeoutList without allocating a new istance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...