bad_alloc occuring when allocating small structs

Posted by SalamiArmi on Stack Overflow See other posts from Stack Overflow or by SalamiArmi
Published on 2010-03-16T21:10:16Z Indexed on 2010/03/16 21:11 UTC
Read the original article Hit count: 311

Filed under:
|
|
|

A bad_alloc has started showing up in some code which looks perfectly valid to me and has worked very well in the past. The bad alloc only occurs once every 50-3000 iterations of the code, which is also confusing.

The code itself is from a singly linked list, simply adding a new element to the queue:

template<typename T> struct container
{
 inline container() : next(0) {}

 container *next;
 T data;
};

void push(const T &data)
{
 container<T> *newQueueMember = new container<T>;

 //... unrelated to crash
}

Where T is:

struct test
{
 int m[256];
};

Changing the size of the array allocated array to anything but very small values (1-8 ints) still results in a bad_alloc occasionally.

A few extra notes about my program: - I used Poco::ThreadPool to thread my program. I've only recently added this functionality, before I had it running with Win32 threads. However, only the main thread ever calls push(). - I am also occasionally getting other crashes which could be related. However, when I try to debug with visual studio 2008, I can't navigate back to the call stack, or the crash happens deep within new().

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about bad-alloc