[LLVMdev] How to handle memory allocation failures (in libstdc++) with disabled exceptions (was Re: [llvm-commits] [polly] r138203 - /polly/trunk/lib/Support/GICHelper.cpp)

Tobias Grosser grosser at fim.uni-passau.de
Wed Aug 24 00:07:36 PDT 2011


On 08/20/2011 06:15 PM, David Blaikie wrote:
>> -  std::string string(isl_printer_get_str(p));
>> +  char *char_str = isl_printer_get_str(p);
>> +  std::string string(char_str);
>> +  free(char_str);
>
> This got me wondering: If this were compiled with exceptions it
> wouldn't be safe (std::string's ctor could throw&  then the free
> wouldn't be called), but I know LLVM doesn't use exceptions in its
> codebase.

You are right. Even though it would not yield to unexpected behavior, we 
would have a memory leak.

> Are exceptions actually disabled in the build? (I assume so)
At least my build uses -fno-exceptions

> How are memory allocation failures handled?
Interesting question. I must admit in this code they are not handled at 
all. I would be very interested in how other people would handle this.

> How do the standard library types function in the absence of
> exceptions? (I've seen Howard Hinnant checking in changes to libc++ to
> support cases where exceptions are disabled&  I was wondering what
> sort of semantics he (&  libstdc++ too) provide when exceptions are
> disabled).

Again a very interesting question. I assume for explicit allocations 
with 'new' libstdc++ would return a NULL pointer (instead of throwing an 
exception).

str::string *str = new std::string("Hallo");

However, I have no idea what happens in the case of:

str::string str("Hallo);

Tobi



More information about the llvm-dev mailing list