Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6671207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:21:37+00:00 2026-05-26T03:21:37+00:00

Question says it all: Are there any pitfalls with allocating exceptions on the heap?

  • 0

Question says it all: Are there any pitfalls with allocating exceptions on the heap?

I am asking because allocating exceptions on the heap, in combination with the polymorphic exception idiom, solve the problem of transporting exceptions between threads (for the sake of discussion, assume that I can’t use exception_ptr). Or at least I think it does…

Some of my thoughts:

  • The handler of the exception will have to catch the exception and know how to delete it. This can be solved by actually throwing an auto_ptr with the appropriate deleter.
  • Are there other ways to transport exceptions across threads?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:21:38+00:00Added an answer on May 26, 2026 at 3:21 am

    Are there any pitfalls with allocating exceptions on the heap?

    One obvious pitfall is that a heap allocation might fail.

    Interestingly, when an exception is thrown it actually throws a copy of the exception object that is the argument of throw. When using gcc, it creates that copy in the heap but with a twist. If heap allocation fails it uses a static emergency buffer instead of heap:

    extern "C" void *
    __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) throw()
    {
        void *ret;
    
        thrown_size += sizeof (__cxa_refcounted_exception);
        ret = malloc (thrown_size);
    
        if (! ret)
        {
            __gnu_cxx::__scoped_lock sentry(emergency_mutex);
    
            bitmask_type used = emergency_used;
            unsigned int which = 0;
    
            if (thrown_size > EMERGENCY_OBJ_SIZE)
                goto failed;
            while (used & 1)
            {
                used >>= 1;
                if (++which >= EMERGENCY_OBJ_COUNT)
                    goto failed;
            }
    
            emergency_used |= (bitmask_type)1 << which;
            ret = &emergency_buffer[which][0];
    
        failed:;
    
            if (!ret)
                std::terminate ();
        }
    }
    

    So, one possibility is to replicate this functionality to protect from heap allocation failures of your exceptions.

    The handler of the exception will have to catch the exception and know how to delete it. This can be solved by actually throwing an auto_ptr with the appropriate deleter.

    Not sure if using auto_ptr<> is a good idea. This is because copying auto_ptr<> destroys the original, so that after catching by value as in catch(std::auto_ptr<std::exception> e) a subsequent throw; with no argument to re-throw the original exception may throw a NULL auto_ptr<> because it was copied from (I didn’t try that).

    I would probably throw a plain pointer for this reason, like throw new my_exception(...) and catch it by value and manually delete it. Because manual memory management leaves a way to leak memory I would create a small library for transporting exceptions between threads and put such low level code in there, so that the rest of the code doesn’t have to be concerned with memory management issues.

    Another issue is that requiring a special syntax for throw, like throw new exception(...), may be a bit too intrusive, that is, there may be existing code or third party libraries that can’t be changed that throw in a standard manner like throw exception(...). It may be a good idea just to stick to the standard throw syntax and catch all possible exception types (which must be known in advance, and as a fall-back just slice the exception and only copy a base class sub-object) in a top-level thread catch block, copy that exception and re-throw the copy in the other thread (probably on join or in the function that extracts the other’s thread result, although the thread that throws may be stand-alone and yield no result at all, but that is a completely another issue and we assume we deal with some kind of worker thread with limited lifetime). This way the exception handler in the other thread can catch the exception in a standard way by reference or by value without having to deal with the heap. I would probably choose this path.

    You may also take a look at Boost: Transporting of Exceptions Between Threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question pretty much says it all. Is there any way to have the
The question says it all. I can't seem to find any recent rails tutorials
The question sort of says it all - is there a function which does
As my question says, is there any possible way to send an email from
Question says it all. Is there a best recipe to follow to disable user
Question says it all, really. My application is a time tracker. It's currently written
Question says it all, I've got a 500,000 line file that gets generated as
Question says it all. I tried browsing through the latest tag but could not
Question says it all. Some quick code examples of usage would be nice.. thanks!
Question says it all. Does anyone know if the following... size_t div(size_t value) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.