Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Exceptions

This topic is 3704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I correctly use exceptions? What if I have 2 allocations of memory, and want both to be checked if memory allocation went OK, do I need 2 try blocks?
try
{
    char *texture = new char[length];
    try
    {
        char *texture_map = new char[length_map];

        // do stuff

        delete [] texture_map;
    }
    catch(bad_alloc&)
    {
        delete [] texture_map;
    }
    delete [] texture;
}
catch (bad_alloc&)
{
    delete [] texture;
}
Or can I somehow place them both in one try block? Then what if only the first allocations went wrong, then it would still delete the second one:
try
{
    char *texture = new char[length];
    char *texture_map = new char[length_map];

    // do stuff

    delete [] texture_map;
    delete [] texture;
}
catch (bad_alloc&)
{
    delete [] texture_map;
    delete [] texture;
}
If the second method is valid, would it be good to just put a try block around my whole frame loop to check for bad allocation? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Using the second one should be fine I believe. I am in no way an expert on Exceptions, but if any one of them gets an exception it will jump to your catch block, and say if you just choose to continue, then it will go again and check your second one automatically. If you need to know specifically which one it is that is failing, you can code that into your exception class also.

Share this post


Link to post
Share on other sites
If you initialize your pointers to NULL first, you can safely call delete on both of them, no matter which allocation failed. The preferred method is to use some kind of smart pointers, such as boost::shared_array, or use std::vector instead.

Share this post


Link to post
Share on other sites
Okay thanks :)

And what if I check for one allocation and it fails, do I need to delete it? It failed so it didn't allocate any memory right?


try
{
pather = new MicroPather(this, 20);
}
catch (bad_alloc&)
{
delete pather;
fatal << "could not allocate memory" << POS;
quit();
}


OR:

try
{
pather = new MicroPather(this, 20);
}
catch (bad_alloc&)
{
fatal << "could not allocate memory" << POS;
quit();
}

Share this post


Link to post
Share on other sites
It's a bit more complicated that this: other exceptions may happen during the execution of your code, which you then must catch and handle. Your typical code will look like this:

// This happens outside the try-catch block: if an error happened,
// there would be no memory to release.
char *texture = new char[length];
try
{
// Attempt to allocate this. If it fails, you need to deallocate
// 'texture'.
char *texture_map = new char[length_map];

try
{
// do stuff
}
catch(...)
{
// Clean up then propagate back up.
delete [] texture_map;
throw;
}

delete [] texture_map;
}
catch (...)
{
// Clean up memory then propagate the exception to where
// it can be handled.
delete [] texture;
throw;
}

delete [] texture;


Of course, std::vector does all of this for you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!