 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hovik Melikyan Guest
|
Posted: Wed Oct 29, 2003 1:29 am Post subject: 'unreachable code' in a throw statement |
|
|
I have a throw statement which looks like
throw new X("You made a boo-boo");
where X is defined like
class X
{
// ...
X(const std::string&);
// ...
}
MSVC7 gives a "unreachable code" warning pointing to the throw statememnt.
The warning disappears as soon as I remove the operator new in the throw
statement OR make the X constructor accept (const char*) instead of (const
string&). So dynamic allocation and the dynamic string together confuse the
..NET compiler.
I looked at the assembly code and found nothing that can be left out when an
exception is thrown. Also, no memory leaks seem to occur.
Can anyone explain if this warning message is erroneous or it really warns
of a potential bug?
Thanks,
--
Hovik Melikyan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Daniel Guest
|
Posted: Wed Oct 29, 2003 4:45 pm Post subject: Re: 'unreachable code' in a throw statement |
|
|
Hovik Melikyan wrote:
| Quote: | Can anyone explain if this warning message is erroneous or it really
warns of a potential bug?
|
I'd suggest posting a minimal complete compiland that produces the warning
to microsoft.public.dotnet.languages.vc where someone will no doubt be able
to determine exactly what's going on. You can post to this group on
news.microsoft.com if your ISP doesn't carry it.
-cd
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Guest
|
Posted: Thu Oct 30, 2003 3:25 pm Post subject: Re: 'unreachable code' in a throw statement |
|
|
| Quote: | I have a throw statement which looks like
throw new X("You made a boo-boo");
where X is defined like
class X
{
// ...
X(const std::string&);
// ...
}
MSVC7 gives a "unreachable code" warning pointing to the throw statememnt.
The warning disappears as soon as I remove the operator new in the throw
statement OR make the X constructor accept (const char*) instead of (const
string&). So dynamic allocation and the dynamic string together confuse the
.NET compiler.
I looked at the assembly code and found nothing that can be left out when an
exception is thrown. Also, no memory leaks seem to occur.
Can anyone explain if this warning message is erroneous or it really warns
of a potential bug?
|
I don't see what the compiler is complaining about. However, you have
a leak. The runtime system copies the pointer to the X object you
create, s.15.1(3-4), and passes it to any handlers. Afterward it
throws away the copy, s.15.1(4), but nothing ever deletes the X object
you created.
Why not use
throw X ("This will not leak");
instead?
-Ron
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hovik Melikyan Guest
|
Posted: Fri Oct 31, 2003 2:56 am Post subject: Re: 'unreachable code' in a throw statement |
|
|
"Ron" <rcrane (AT) ictv (DOT) com> wrote
| Quote: |
I don't see what the compiler is complaining about. However, you have
a leak. The runtime system copies the pointer to the X object you
create, s.15.1(3-4), and passes it to any handlers. Afterward it
throws away the copy, s.15.1(4), but nothing ever deletes the X object
you created.
|
The catcher is responsible for deleting the object, of course.
Anyway, it's been confirmed as a compiler bug already at
microsoft.public.dotnet.languages.vc.
| Quote: |
Why not use
throw X ("This will not leak");
instead?
|
Well, sometimes you have a complex exception object and you don't want it to
be copied multiple times while rewinding the stack. You gain some
performance at the price of 'delete x' in the final catcher. Why not?
--
Hovik Melikyan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Sat Nov 01, 2003 9:56 am Post subject: Re: 'unreachable code' in a throw statement |
|
|
Hi,
| Quote: | Well, sometimes you have a complex exception object and you don't want it to
be copied multiple times while rewinding the stack. You gain some
performance at the price of 'delete x' in the final catcher. Why not?
|
Well, if performance (!) is an issue, one shouldn't use exceptions in first
place. Throwing an exception should be, errr, the exceptional case, right. (-;
Stack unwinding takes its time. And passing a pointer is at least
error prone, so I really don't see why this could be a good thing to do.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Volker Glave Guest
|
Posted: Sat Nov 01, 2003 9:57 am Post subject: Re: 'unreachable code' in a throw statement |
|
|
"Hovik Melikyan" <hovik (AT) melikyan (DOT) com> wrote
| Quote: | "Ron" <rcrane (AT) ictv (DOT) com> wrote in message
news:c668c1f8.0310291328.14284b95 (AT) posting (DOT) google.com...
I don't see what the compiler is complaining about. However, you have
a leak. The runtime system copies the pointer to the X object you
create, s.15.1(3-4), and passes it to any handlers. Afterward it
throws away the copy, s.15.1(4), but nothing ever deletes the X object
you created.
The catcher is responsible for deleting the object, of course.
Anyway, it's been confirmed as a compiler bug already at
microsoft.public.dotnet.languages.vc.
Why not use
throw X ("This will not leak");
instead?
Well, sometimes you have a complex exception object and you don't want
it to be copied multiple times while rewinding the stack. You gain some
performance at the price of 'delete x' in the final catcher. Why not?
|
Because the final catcher in general is not supposed to, and should not,
and does not have knowledge about if the object was or was not created
by "new" in the first place. (Except if he does. So do what you want to.
If it's working it's good enough.)
Volker Glave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ali Cehreli Guest
|
Posted: Sat Nov 01, 2003 10:29 am Post subject: Re: 'unreachable code' in a throw statement |
|
|
"Hovik Melikyan" <hovik (AT) melikyan (DOT) com> wrote
| Quote: | "Ron" <rcrane (AT) ictv (DOT) com> wrote in message
news:c668c1f8.0310291328.14284b95 (AT) posting (DOT) google.com...
I don't see what the compiler is complaining about. However, you have
a leak. The runtime system copies the pointer to the X object you
create, s.15.1(3-4), and passes it to any handlers. Afterward it
throws away the copy, s.15.1(4), but nothing ever deletes the X object
you created.
The catcher is responsible for deleting the object, of course.
|
But the catcher can never be sure whether the object is allocated on
the heap. What if someone writes
throw X("...");
The catcher can still catch by 'X const *' but should not delete.
| Quote: | Why not use
throw X ("This will not leak");
instead?
Well, sometimes you have a complex exception object and you don't want it to
be copied multiple times while rewinding the stack. You gain some
performance at the price of 'delete x' in the final catcher. Why not?
|
If the catchers catch by 'X const &', there is no copy. There should
be only one exception object constructed in the special memory area
that is used for thrown objects.
The standard idiom is to throw by value and catch by const reference.
Scott Meyers's 'More Effective C++', item 13, is specifically on this
subject.
Ali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hovik Melikyan Guest
|
Posted: Sun Nov 02, 2003 1:20 am Post subject: throw new ... [Was: 'unreachable code' in a throw statement] |
|
|
"Ali Cehreli" <acehreli (AT) yahoo (DOT) com> wrote
| Quote: |
But the catcher can never be sure whether the object is allocated on
the heap. What if someone writes
throw X("...");
The catcher can still catch by 'X const *' but should not delete.
|
'X const *' won't catch it. Actually 'X const &' will, like you said
(below).
| Quote: |
If the catchers catch by 'X const &', there is no copy. There should
be only one exception object constructed in the special memory area
that is used for thrown objects.
|
Well, that works fine except some rather exotic situations when an exception
object can live 'beyond' the scope of exception handling. I'm experimenting
with some non-trivial error recovery methods and also with using C++
exceptions for returning from nested calls.
Exceptions are intened for exceptional situations, that's true, but it's
conventional. It's not obvious that exceptions can't be used for returning
values from 'deeply' nested functions, if they can replace couple of
branches and return's. For the sake of simplicity and readability of the
source code.
In this regard 'throw new ...' makes sense as long as 'return new ...' makes
sense.
I'd appreciate any opinions and/or references on this topic.
Thanks,
--
Hovik Melikyan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Sun Nov 02, 2003 4:50 pm Post subject: Re: 'unreachable code' in a throw statement |
|
|
[email]acehreli (AT) yahoo (DOT) com[/email] (Ali Cehreli) wrote in message news:<f0070508.0310311621.4bdf7cf7 (AT) posting (DOT) google.com>...
| Quote: | But the catcher can never be sure whether the object is allocated on
the heap. What if someone writes
throw X("...");
The catcher can still catch by 'X const *' but should not delete.
|
This is incorrect. Catch statements match similar to function calls.
If you call a function passing in an X, it won't bind to an X*.
Likewise, with exceptions, if you throw an instance, you can catch by
copy/value or reference - if you throw a pointer, you must catch by
pointer.
joshua lehrer
factset research systems
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|