bad_alloc when calling new on class Texture

1k views Asked by At

This is the offending line:

Texture *texture = new Texture (...);

I receive from bad_alloc here:

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
    {       // try to allocate size bytes
    void *p;
    while ((p = malloc(size)) == 0)
            if (_callnewh(size) == 0)
            {       // report no memory
            static const std::bad_alloc nomem;
            _RAISE(nomem);
            }

    return (p);
    }

size is ~28 bytes big

and so far the program has placed maybe 2 MB on the heap in a 32bit system (fresh reboot) and before this only about twenty things get allocated on the heap so I know there's no heap corruption.

I'm so confused...

3

There are 3 answers

0
trojanfoe On

According to MSDN, _callnewh():

This function throws bad_alloc if the new handler can’t be located.

So you haven't correctly installed the 'new handler' using _set_new_handler().

0
User 154806 On

I think for this issue you can refer to >>Item 07 of Effective C++.

0
Andrea Bergia On

Heap corruption doesn't necessarily mean "too much memory allocated"; rather, it often means that you have screwed up with some pointers.

Check whether you made some mistakes like that, since you are saying that you haven't exhausted the memory.