简体   繁体   English

段 指向结构体指针向量的指针导致的退出错误

[英]Seg. Fault on exit caused by pointer to vector of pointers to struct

After a few days of delaying this question and debugging I've found that If my code below is run after removing the vector from the union (and all of the code mentioning this vector) the seg. 经过几天的延迟和调试后,我发现,如果从联合中删除向量 (以及提及该向量的所有代码)后,下面的代码运行了。 fault dissapears. 故障消失。

#include <string>
#include <vector>
#include <functional>

struct Task
{
  std::string Name;

  std::vector<Task*> *Subtasks;
  std::function<void(std::string const &)> Job;

  Task() {}
  Task(std::string const &arg_0) { Name = arg_0; }
  Task(std::string const &arg_0, std::vector<Task*> *arg_1) { Name = arg_0; Subtasks = arg_1; }
  Task(std::string const &arg_0, std::function<void(std::string const &)> arg_1)
  { Name = arg_0; Job = arg_1; }

  ~Task() { for (auto tItem : *Subtasks) { delete tItem; } }
};

class Console
{
private:
  std::vector<Task*> Routine;

public:
  ~Console() { for (auto tItem : Routine) { delete tItem; } } //I thought that this is not needed but Valgrind thinks otherwise, strangely the deconstructors of the Tasks are not called, I guess.

  void add(Task *arg_0) { Routine.push_back(arg_0); }

  void foo()
  {
    Task *tTask = new Task();
    //Task *tTask = new Task("Name");
    //Task *tTask = new Task("Name", [this](std::string const &arg_0){ ; });
   //Seg. fault still remains.
    add(tTask);
  }
};

int main()
{
    Console _Console;
    _Console.foo();
}

For quick online IDE code test 快速在线IDE代码测试


This should probably be another question, but I think it's too simple. 这可能是另一个问题,但我认为这太简单了。 I've heard before, that if a non-trivial is used in a union, it should be taken care of if the values are switched, how would one do that, and is it necessary if I do not intend to have any value changes on runtime? 之前我曾听说过,如果在工会中使用了非平凡的事物,那么应该注意是否切换了值,如何进行切换以及如果我不打算进行任何值更改,则有必要这样做在运行时?

EDIT1 编辑1

Removed the union of vector and std::function 删除了vector和std :: function的并集

EDIT2 编辑2

The Seg. 段。 Fault is most likely caused by the deconstructor ~Task(); 错误很可能是由解构函数〜Task ()引起的 .

If this constructor is called: 如果调用此构造函数:

Task(std::string const &arg_0, std::function<void(std::string const &)> arg_1)
{ Name = arg_0; Job = arg_1; }

then it crashes for sure when destructor is called because Subtasks is not initialized. 那么它崩溃肯定时,析构函数被调用,因为Subtasks未初始化。

~Task() { for (auto tItem : *Subtasks) { delete tItem; } }

Fix: 固定:

Task(std::string const &arg_0, std::function<void(std::string const &)> arg_1)
{ Name = arg_0; Job = arg_1; Subtasks = nullptr;}

~Task() { if (Subtasks!=nullptr) for (auto tItem : *Subtasks) { delete tItem; } }

and (thx for the comments) gather/replace and fix all other constructors like this: 和(thx为注释)收集/替换并修复所有其他构造函数,如下所示:

 Task(std::string const &arg_0 = "", std::vector<Task*> *arg_1 = nullptr) { Name = arg_0; Subtasks = arg_1; }

After some discussion, it appears that there's a possible design flaw in the code of the question: deleting the contents of the vector is dangerous since the pointers may be still in use by other clients, or may not even been allocated, but just reference local variables. 经过一番讨论,似乎该问题的代码中存在一个设计缺陷:删除向量的内容很危险,因为指针可能仍被其他客户端使用,甚至可能未被分配,而只是引用本地变量。 And why Subtasks is a pointer in the first place? 以及为什么Subtasks是指针? Why not deleting it? 为什么不删除它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM