Question

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

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.

This is the function where the code is crashing:

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+++
...

This is the output:

> 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?

Platform: Windows 7, MinGW 4.4, GCC

Was it helpful?

Solution 2

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.

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?

OTHER TIPS

To add to this (because first Google result): In my szenario i got bad_alloc even when i still had a few GBs of RAM available.

If your application needs more than 2GB of memory you have to enable the /LARGEADDRESSAWARE option in the Linker settings.

If you need more than 4GB you have to set your build target to x64 (in Project Settings and the Build configuration)

Due to how the automatic resizing of vectors works you will hit the breakpoints at ~1gb / ~2gb vector size

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top