简体   繁体   English

C + +结构的向量与结构中的向量导致异常

[英]C++ vector of struct with vector in struct causing exception

I got a problem with the following code, which I can't solve since hours... I'm glad for any kind of advice. 我遇到了以下代码的问题,这是我几个小时以来无法解决的...我很高兴能收到任何建议。

The types are defined as follows: 类型定义如下:

typedef std::vector< cv::RotatedRect > ds_t;

typedef struct id_ {
std::string path;
std::string fileName;
} id_t;

typedef struct dd_ {
    cv::RotatedRect bB;
} dd_t;

typedef std::pair<id_t, std::vector<dd_t> > ts_t;

typedef std::vector<ts_t> tss_t;

Then I try to fill the type with data: 然后,我尝试用数据填充类型:

tss_t samples
while (readdir(pDir) != NULL) {
     ts_t sample;
     sample.first.path = _path;
     sample.first.fileName = _name;
     ds_t detections;
     getDetections(_path, detections); //this only filles the detecions byref
     for(size_t i=0; i<detections.size(); i++) {
         dd_t data;
         data.bB = detections[i];
         sample.second.push_back(data); //TODO FIXME
     }
     samples.push_back(sample);
 }

cv::RotatedRect is a very basic class. cv :: RotatedRect是一个非常基本的类。

class RotatedRect{
   public:
   RotatedRect();
   RotatedRect(const Point2f& _center, const Size2f& _size, float _angle);
   void points(Point2f pts[]) const;

   Point2f center;
   Size2f size;
   float angle; 
};

bool getDetections(const std::string &imagePath, ds_t &detections)
{
  //some processing
  for(size_t i=0;i<5;i++)
    detections.push_back(RotatedRect(cv::Point2f(110.,110.), cv::Size2f(10.,10.), 90.0f));

  return true;
}

Hopefully I copied the whole code and I know, I don't need most of the typedefs... 希望我复制了整个代码,而且我知道,我不需要大多数的typedef ...

I have already tried to reserve space sample.second.reserve(detections.size()), but this only postpones the error to samples.push_back. 我已经尝试过保留空间sample.second.reserve(detections.size()),但这只会将错误推迟到samples.push_back。

The erroneous line is indicated by the FIXME, causing "terminate called after throwing an instance of 'std::bad_alloc'" FIXME指示错误的行,导致“抛出'std :: bad_alloc'实例后调用终止”

Thanks for any advice in advance. 感谢您提前提出任何建议。

std::bad_alloc generally means that you've run out of memory -- possibly because of a leak, or just because you've allocated too much. std::bad_alloc通常意味着您内存不足-可能是由于泄漏,或者仅仅是因为您分配了太多内存。 It can also (rarely) happen due to heap corruption (running off the end of an allocated array or dereferencing a pointer after it has been deleted) or other undefined behavior at some earlier point in the program. 它也可能(很少)由于堆损坏(在分配的数组的末尾运行或在删除数组后取消对指针的引用)或程序中某个较早位置的其他未定义行为而发生。

How much memory is your program trying to use at one time? 您的程序一次要使用多少内存? You can try using a leak detector like valgrind to see if there is stuff you should be cleaning up. 您可以尝试使用类似valgrind的检漏仪来查看是否有东西需要清理。

edit 编辑

The -1 to __builtin_new tells you a lot -- it tells you that someone is calling new with a bogus size. -1 to __builtin_new-1 to __builtin_new告诉您很多信息-它告诉您有人在以虚假大小呼叫新呼叫。 That would probably be a member of std::vector that tries to resize things (you can check the stack trace from valgrind or use a debugger to be sure), which indicates that the vector has been corrupted. 这可能是std :: vector的成员,该成员试图调整大小(您可以从valgrind检查堆栈跟踪或确定使用调试器),这表明向量已损坏。 Since sample.second is a local (on stack) variable, that tells you that a previous function you called (probably getDetections ) overran an onstack buffer or array of some kind and clobbered sample.second . 由于sample.second是局部(堆栈)变量,告诉你,你所谓的(可能是以前的功能getDetections )占领了某种类型的onstack缓冲区或数组和惨败sample.second So take a careful look at what that function is doing -- the code you've commented out as //some processing . 因此,请仔细查看该函数的功能-您注释掉的代码//some processing You can also try using a debugger to set a breakpoint just after sample is created and then set a watchpoint on the memory used by sample.second that is getting corrupted. 您还可以尝试在创建sample后立即使用调试器设置断点,然后在sample.second损坏的内存上设置观察点。 Then continue the program and it should stop at the point that is clobbering sample.second 然后继续执行程序,它应该在破坏sample.second的位置停止。

It may be helpful to read your compiler's header files to figure out how it implements std::vector -- there's probably a couple of size_t fields and a pointer field in there. 读取编译器的头文件以了解其如何实现std :: vector可能会有所帮助-那里可能有几个size_t字段和一个指针字段。

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

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