我正在做一个神经元模拟项目,我有一个我无法理解的 seg 错误,gdb 也是如此。我正在使用智能指针
我有这样的东西:
class vehicle:enable_shar_from_this() {
protected
double direction_;
}
class car: public direction_ {
friend class policeAgent;
public:
void change_direction(rnd);
}
class policeAgent: enable_share... {
public:
std::vector<CarSmartPointer> cars ={};
void close_the_street(rnd);
}
car::change_direction(rnd_num){
OLD_direction=direction;
direction+=direction+rnd_num;
newCar=make_shared<CarSmartPointer>();
arriving_cars.push_back(newCar);
}
publicAgent::close_the_street(rnd_num){
lastCar= cars.cend();
for (myCar=cars.begin(); myCar!=lastCar; myCar++)
{(*myCar)->change_direction(rnd_num);}
main{
auto sbirro makeshared<publicAgent>();
while (not cars.empty()){
sbirro->closeTheStreet()
}
}
所有变量都在正确的构造函数中初始化,该程序运行 100-150 个周期,然后它出现段错误!! 当分配OLD_direction=方向时发生段错误 我试图打印"方向"但段错误,我确定它在那里,因为我之前打印了一行 它不会发生在容器的末尾。这不是最后一辆车,而是第二辆车,它断层
这是回溯,程序很大,我正在使用 OpenMP,但在这种情况下,它根本没有被调用。 我无法逐步调试,因为我使用的是 python 接口,并且我在另一台计算机上拥有主要的.cpp并且初始化整个程序是许多行代码......我明天就去。
Thread 1 "python3" received signal SIGSEGV, Segmentation fault.
增长0x00007ffff6319675:增长锥::增长(这=0x0,rnd_engine=...) 在/。。my_folders../元素/生长锥.cpp:52
52双store_angle=angle_;
gdb) bt
#0 0x00007ffff6319675 in
growth::GrowthCone::grow(std::mersenne_twister_engine<unsigned long,
32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul,
2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) (this=0x0,
rnd_engine=...)
at /..my_folders../elements/GrowthCone.cpp:52
#1 0x00007ffff631ce2e in growth::Neurite::update_growth_cones(std::mersenne_twister_engine<unsigned
long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul,
2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) (this=0x983700,
rnd_engine=...)
at /..my_folders../elements/Neurite.cpp:58
#2 0x00007ffff631cd70 in
growth::Neurite::grow(std::mersenne_twister_engine<unsigned long, 32ul,
624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul,
15ul, 4022730752ul, 18ul, 1812433253ul>&) (this=0x983700, rnd_engine=...)
at /..my_folders../elements/Neurite.cpp:44
#3 0x00007ffff631245d in
growth::Neuron::grow(std::mersenne_twister_engine<unsigned long, 32ul,
624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul,
15ul, 4022730752ul, 18ul, 1812433253ul>&) (this=0x724390, rnd_engine=...)
at /..my_folders../elements/Neuron.cpp:120
#4 0x00007ffff630f37f in
_ZN6growth17SimulationManager8simulateERKNS_4TimeE._omp_fn.0(void) ()
at /..my_folders../kernel/simulation_manager.cpp:110
#5 0x00007ffff535e15f in GOMP_parallel (fn=0x7ffff630f251
<_ZN6growth17SimulationManager8simulateERKNS_4TimeE._omp_fn.0(void)>,
data=0x7fffffffd010, num_threads=1, flags=0) at
/build/gcc/src/gcc/libgomp/parallel.c:168
#6 0x00007ffff630efeb in
growth::SimulationManager::simulate(growth::Time const&) (this=0x99b2b0,
t=...)
谢谢
我在循环时修改容器的问题,所以这是一个未定义的行为。
解决方案添加一个临时容器来存储新车,然后插入旧容器!