从std向量中的push_back()崩溃 [英] Crash from a push_back() in a std vector

查看:2593
本文介绍了从std向量中的push_back()崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此计划的工作原理如下:

This program works as expected:

#include <iostream>
#include <string>
#include <vector>    
using namespace std;

struct Thumbnail
{
    string  tag;
    string  fileName;
};

int main()
{
    {
        Thumbnail newThumbnail;
        newThumbnail.tag = "Test_tag";
        newThumbnail.fileName = "Test_filename.jpg";

        std::vector<Thumbnail> thumbnails;

        for(int i = 0; i < 10; ++i) {
            thumbnails.push_back(newThumbnail);
        }
    }
    return 0;
}

如果我复制并粘贴主代码块在另一个项目线程),在任何函数内,我得到这个异常从行注释 // < - 第二个循环崩溃

If I copy and paste the main block of code in another project (still single threaded), inside any function, I get this exception from the line commented // <-- crash at the 2nd loop:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

如果我在任何push_back之前清除向量,一切都是正确的(但这当然不是期望的行为)。这使我认为它是如果向量不能存储多个这样的对象。

If I clear the vector before any push_back, everything is all right (but of course this is not the desired behaviour); this makes me think that it is like if the vector could not store more than one such object.

这是代码崩溃的函数:

int ImageThumbnails::Load(const std::string &_path)
{
    QDir thumbDir(_path.c_str());

    if(!thumbDir.exists())
        return errMissingThumbPath;

    // Set a filter
    thumbDir.setFilter(QDir::Files);
    thumbDir.setNameFilters(QStringList() << "*.jpg" << "*.jpeg" << "*.png");
    thumbDir.setSorting(QDir::Name);

    // Delete previous thumbnails
    thumbnails.clear();

    Thumbnail newThumbnail;

    ///+TEST+++
    {
        Thumbnail newThumbnail;
        newThumbnail.tag = "Test_tag";
        newThumbnail.fileName = "Test_filename.jpg";

        std::vector<Thumbnail> thumbnails;

        for(int i = 0; i < 10; ++i)
        {
            TRACE << i << ": " << sizeof(newThumbnail) << "  /  " << newThumbnail.tag.size() << " / " << newThumbnail.fileName.size() << std::endl;
            //thumbnails.clear();                   // Ok with this decommented
            thumbnails.push_back(newThumbnail);     // <-- crash at the 2nd loop
        }

        exit(0);
    }
    ///+TEST+END+++
...

这是输出:

> TRACE: ImageThumbnails.cpp:134:Load  
0: 8  /  8 / 17
> TRACE: ImageThumbnails.cpp:134:Load  
1: 8  /  8 / 17
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

为什么在两个不同的项目中为同一段代码得到不同的行为?

Why do I get this different behaviour for the same piece of code in two different projects?

平台:Windows 7,MinGW 4.4,GCC

Platform: Windows 7, MinGW 4.4, GCC

推荐答案

它在另一个应用程序中使用完全相同的代码时崩溃,有可能程序是内存不足(std :: bad_alloc异常可能是因为这个)。检查其他应用程序使用的内存量。

If it is crashing when using the exact same code in another application, there is the possibility that the program is out of memory (std::bad_alloc exceptions can be because of this). Check how much memory your other application is using.

另一件事...使用std :: vectors时,使用reserve()方法,元素将被推入向量。看起来你正在推动完全相同的元素10次。为什么不使用包含默认对象参数的resize()方法?

Another thing ... use the reserve() method when using std::vectors and you know ahead of time how many elements are going to be pushed into the vector. It looks like you are pushing the exact same element 10 times. Why not use the resize() method that includes the default object parameter?

这篇关于从std向量中的push_back()崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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