SIGNALSEGV(分段错误)矢量外推回方法 [英] SIGNALSEGV (segmentation fault) out of vector pushback method

查看:31
本文介绍了SIGNALSEGV(分段错误)矢量外推回方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 linux (WSL) 制作一个 shell.出于某种未知的原因,当我将一个结构体指针(我创建的)推回到一个向量中时,它会调用 SIGNALSEGV.

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

};

这是 TimeoutCommand 构造函数和调用有问题函数的行:

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.

运行这个命令看起来像这样:超时 3 睡眠 10

running this command woul look something like: timeout 3 sleep 10

哪个将是 cmd_line

Which will be the cmd_line

这是 std::vector 中导致段错误的部分

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 ** **

推荐答案

问题是我在没有分配新实例的情况下初始化了一个指向 TimeoutList 实例的指针.

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

这篇关于SIGNALSEGV(分段错误)矢量外推回方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆