将值从(共享指针的)向量分配给共享指针会导致分段错误 C++ [英] assigning value from a vector (of shared pointer) to a shared pointer cause segmentation fault c++

查看:65
本文介绍了将值从(共享指针的)向量分配给共享指针会导致分段错误 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个 vector <vector <vector 名为 foosBoxes.嵌套向量具有模拟物理框位置的作用.我还有一个导致分段错误的 while 循环:

In my code, I have a vector <vector <vector <vector <std::tr1::shared_ptr<foo> > > > > named foosBoxes. The nested vector has a role of simulating a physical boxes position. I also have a while loop which cause a segmentation fault:

vector<std::tr1::shared_ptr<foo> >::iterator fooit = foosBoxes[x][y][z].begin(); //x,y,z are valid integer
std::tr1::shared_ptr<foo> aFoo;
while (fooit != foosBoxes[x][y][z].end()){
  aFoo = *fooit; //this cause segmentation fault
  fooit++;
  //some stuff which does not have an effect on fooit;
}

我尝试过的一些事情:
1. 我曾尝试使用 aFoo = *fooit++ 但这没有用.
2.分段错误大约发生在千分之几的循环之后,它继续正常.
3. 我已经尝试过 valgrind 问题并且 valgrind 完成了步骤.
4.在崩溃的循环中,我在可疑行前后打印了一个运行计数器.在行前,我得到 8 次打印(矢量的大小),而在我得到 7 次打印后.

Some thing I have tried:
1. I have tried to use aFoo = *fooit++ but this didn't work.
2. The segmentation fault occurs roughly after several thousandths loops which went on fine.
3. I have tried to valgrind the problem and valgrind went through the step.
4. In the loop that crashes, I have printed a running counter before and after the suspected line. when before the line, I get 8 printings (the size of the vector) and when after I get 7 printings.

我该如何解决这个问题?

How can I figure this out?

更新:
我在主循环之前添加了一个循环:

Update:
I have added a loop to run before the main loop:

int kkk = 1214
int c = 0;
while (c < foosBoxes[x][y][z].end()){
   aFoo = foosBoxes[x][y][z][c++];
   printf("%i\t, kkk);
   fflush(stdout);
}

产生相同的结果.

更新:
根据gdb:

程序收到信号SIGSEGV,分段错误.0x000000000043e400 在旋转 (kkk=1214) 在/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:153第153话

Program received signal SIGSEGV, Segmentation fault. 0x000000000043e400 in rotate (kkk=1214) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:153 153 dispose();

我认为 boost_shared_ptr.h 中合适的函数是

I think that the appropriate function in boost_shared_ptr.h is

void
  release() // nothrow                                                                                                                      
  {
    if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
      {
        dispose(); //this is line 153
#ifdef __GTHREADS
        _GLIBCXX_READ_MEM_BARRIER;
        _GLIBCXX_WRITE_MEM_BARRIER;
#endif
        if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
          destroy();
      }
  }

dispose() 在文件的其他地方定义:

dispose() is defined elsewhere in the file:

  // dispose() is called when _M_use_count drops to zero, to release                                                                        
  // the resources managed by *this.                                                                                                        
  virtual void
  dispose() = 0; // nothrow  

会不会是shared_ptr管理不善,需要切换回普通指针?

Could it be that the reason is ill management of shared_ptr and I should switch back to regular pointer?

更新:
另一个具有类似结果的测试:

Update:
yet another test with similar result:

int kkk = 1214int c = fooBoxes[x][y][z].size();而 (c >= 0){aFoo = foosBoxes[x][y][z][c--];printf("%i\t, kkk);fflush(标准输出);}

int kkk = 1214 int c = fooBoxes[x][y][z].size(); while (c >= 0){ aFoo = foosBoxes[x][y][z][c--]; printf("%i\t, kkk); fflush(stdout); }

这次程序在第三次迭代中崩溃了.如果问题出在错误的分配上,那么程序应该在第一次迭代中崩溃(相反,程序在第一次迭代中崩溃).

This time the program crush in the third iteration. Should the problem was with wrong allocation, then the program should have crush in the first iteration (in the opposite direction the program crashes in the first iteration).

推荐答案

我得出的结论是,问题在于使用 vector 是错误的,因为我通过代码更新了它.我不知道 c++ 中的内存管理是如何工作的,但我相信两个向量之间发生了某种重叠.我已经切换到 set,现在一切正常

I have came to the conclusion that the problem was with the use vector is wrong since I update it through the code. I don't know how the memory management in c++ works but I believe that some sort of overlapping between two vector occurred. I have switched to set and everything works now

这篇关于将值从(共享指针的)向量分配给共享指针会导致分段错误 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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