Re: RFC: (in head) Throws()?

From: Kinkie <gkinkie_at_gmail.com>
Date: Mon, 13 Oct 2008 14:02:25 +0200

On Mon, Oct 13, 2008 at 1:26 PM, Amos Jeffries <squid3_at_treenet.co.nz> wrote:
> Kinkie wrote:
>>
>> Hi all.
>>
>> I've recently found myself battlign with a certain difficulty in
>> formatting custom messages for thrown exceptions; in order to use
>> TextExceptions properly I'd have to set a stringstream up, fill it in,
>> get a c_str out and then Throw, leaking memory in the meantime because
>> the message would be double-copied and there'd be no reachable code to
>> free one of the copies. The current Throw() implementation is very
>> skewed towards servicing Must().
>>
>> All of this should be easily solvable by defining a Throws() macro
>> (possibly with an accompanying Musts call) which would offer semantics
>> similar to debugs(), and get rid of this wart in one easy sweep.
>>
>> Any opinions?
>>
>
> Um I don't see what your problem is. Or what you are trying to do.
>
> To me it looks like is highly geared around non-dependable char* and Must()
> is warped to fit TextException.
>
> "TextException::TextException(msg)"
> can take a local char buffer[], "constant string", or object.c_buf().
> it does xstrdup() and xfree() it properly on its own.

Maybe it's me not seeing the obvious.

How would you render an exception saying
"out of bounds (trying to access index X of Y)"
where X and Y are off_t 's?

What I have tried using was
ostringstream foo;
foo << "out of bounds (trying to access index " << X << " of " << Y << ")";
Throw(foo.str().c_str(),__FILE__,__LINE__);

Which will leak, as the c_str is supposed to be freed by the caller,
but there's no code executed after the Throw.

proposal would be to do something like:
Throws("out of bounds (trying to access index " << X << " of " << Y <<
")",__FILE__,__LINE__);
which would also not leak.

-- 
    /kinkie
Received on Mon Oct 13 2008 - 12:02:28 MDT

This archive was generated by hypermail 2.2.0 : Tue Oct 14 2008 - 12:00:05 MDT