Bad_alloc when calling new in the Texture class

This is an offensive line:

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

I get 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 ~ 28 bytes

and so far the program has put maybe 2 MB in a heap on a 32-bit system (new reboot), and before that, only about twenty things are allocated on the heap, so I don’t know that it is not a heap.

I'm so confused ...

+4
source share
3 answers

Damage to the heap does not necessarily mean "too much allocated memory"; rather, it often means you squinted at some pointers.

Check if you made such mistakes, as you say that you have not run out of memory.

+1
source

According to MSDN, _callnewh ():

This function calls bad_alloc if a new handler cannot be located.

So, you did not correctly install the β€œnew handler” with _set_new_handler() .

0
source

I think that for this problem you can refer to β†’ Point 07 of Effective C ++.

0
source

Source: https://habr.com/ru/post/1338808/


All Articles