🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

My program crashes on a new operator

Started by
13 comments, last by theScore 3 years, 6 months ago

Hello !

I have a simple question : what can I do if a new operator or make_unique fail to allocate and crash ?

How can I avoid a crash and allocate without any problem ?

Advertisement

theScore said:
I have a simple question : what can I do if a new operator or make_unique fail to allocate and crash ?

ask the government to help -lol-

anyway; try this:

std::unique_ptr<MyCameraClass> c;
c = std::make_unique<MyCameraclass>(arg1, arg2); // it will use new internally

hope this helps ?

It is ever the code I am using, it crashes on the line of the make_unique instruction.

Do you think it could be a problem of memory fragmentation ? I am doing a lot of allocation/deallocation with this pointer.

theScore said:
Do you think it could be a problem of memory fragmentation ?

it sounds like you have overloaded the new operator to use a custom memory pool allocator, if this is the case then yes you need to check how you're allocation/deallocating your pool chunks in your pool allocator (pointer violation or what not…);

if you're not using a custom allocator then check the constructors of the class you are trying to instantiate;

you can also try this (make sure you have exceptions throwing enabled for this to work):

// pseudo
try
{
 ... code i gave u OR your code ...
}
catch(std::exception* e) // OR catch(std::exception& e) whichever works for u
{
  ... log problem here
}

u may have to do the same thing in your pool allocator, if it has exception-throwing ability;

in all cases the code i gave u will never crash on its own merit, it is standard code ?

That's it … all the best ?

Thanks, but I have a question : how to do for implement a pool allocator ? Do you know a good website which explain this ?

theScore said:
how to do for implement a pool allocator ? Do you know a good website which explain this ?

the explanation would be too long for this post, so take a look here:

if the links don't work, just copy-paste them into your browser;

u now have enough to get u going;

Have fun ?

Thank you !

Generally, when -new- operator fails, it's because a memory was bad desallocated before.

For example, “delete [] data” when data is not an array. etc.

Thanks for the answer, I will check my code.

The most likely candidates for what causes new/malloc to fail are double deletion (deleting the same object twice) and writing outside allocated object memory (array indexes, pointer math, memcpy()). Both of these can be very hard to spot. That's why tools like valgrind and asan/ubsan exists and using them are recommended practice. I'm not sure of those are available on Windows but their equivalents must be.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement