473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

g++ 3.4 exception problem

I have a case where an exception in the constructor of class with a
virtual base leads to termination:

struct vbase {};

struct foo : virtual vbase {
foo() {
throw "exception in foo ctor";
}
};

main()
{
struct bar : public foo {};
try {
bar a;
}
catch ( ... ) {
}
return 0;
};

This program demonstrates the problem in g++ 3.4. Instead of catching
the exception, the program terminates. The base dtor gets called as
expected, but upon return, there is some generated code that I can't
decipher, which jumps to some termination code at bad_alloc + 80.

That should be a clue... but I'm unable to catch it.

|
| markn at ieee dot org
|

Aug 12 '05 #1
11 4183
sn********@gmail.com wrote:
I have a case where an exception in the constructor of class with a
virtual base leads to termination:

struct vbase {};

struct foo : virtual vbase {
foo() {
throw "exception in foo ctor";
}
};

main()
{
struct bar : public foo {};
try {
bar a;
}
catch ( ... ) {
}
return 0;
};

This program demonstrates the problem in g++ 3.4. Instead of catching
the exception, the program terminates. The base dtor gets called as
expected, but upon return, there is some generated code that I can't
decipher, which jumps to some termination code at bad_alloc + 80.

That should be a clue... but I'm unable to catch it.

|
| markn at ieee dot org
|


Suggestion: try with Gcc 4.0.1. Ver 3.4 seems weird at times, and I think
the effort will be focused on version 4 from now on.

Giancarlo.

Aug 13 '05 #2
Same problem appears in g++ 4.0.0... I'd really like to know whether
this is a compiler problem or me doing something wrong...

Aug 13 '05 #3

<sn********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have a case where an exception in the constructor of class with a
virtual base leads to termination:
#include <stdexcept>

struct vbase {};

struct foo : virtual vbase {
foo() {
throw "exception in foo ctor";
Thats not an exception object you are throwing. Try:

throw std::exception("foo ctor");
}
};

main()
int main()
{
struct bar : public foo {};
The struct bar type definition above goes outside of main's body.
try {
bar a;
}
catch ( ... ) {
}
catch (const std::exception& e)
{
e.what();
}
return 0;
};

This program demonstrates the problem in g++ 3.4. Instead of catching
the exception, the program terminates. The base dtor gets called as
expected, but upon return, there is some generated code that I can't
decipher, which jumps to some termination code at bad_alloc + 80.

That should be a clue... but I'm unable to catch it.


Aug 13 '05 #4
* Peter Julian:

<sn********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have a case where an exception in the constructor of class with a
virtual base leads to termination:


#include <stdexcept>

struct vbase {};

struct foo : virtual vbase {
foo() {
throw "exception in foo ctor";


Thats not an exception object you are throwing. Try:

throw std::exception("foo ctor");


Any type can be used.

}
};

main()


int main()
{
struct bar : public foo {};


The struct bar type definition above goes outside of main's body.


AFAICS it's perfectly OK placed where it is. The main restrictions on a
local class are that it cannot have static members, and that it cannot be
used as a template parameter. I cannot find any statement that forbids a
local class from using virtual inheritance.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 13 '05 #5
>Thats not an exception object you are throwing. Try:

No, it's not, but of course there is nothing special about what type of
object you throw, they're all the same as far as the compiler is
concerned.

I have tried quite a few variations on the type of object to be thrown
and various catch clauses, and they all behave the same. I have
verified that std::exception behaves no differently, same error. Only
difference is the error report:

terminate called after throwing an instance of 'std::exception'
what(): St9exception

No additional clues here pointing towards a fix, either.
The struct bar type definition above goes outside of main's body.


I reserve the right to define my structs anywhere I please. :-)

Seriously, the struct definition is legal as is. But, hoping that you
were on to something, I move the struct definition to the global
namespace, no joy.

I'm still searching for a workaround, no luck so far... I think if I
was able to step through the unwind source code where things go awry I
might be able to figure it out - the unannotated assembly is a bit
tough.. still working on how to do that.
|
| Mark
|

Aug 13 '05 #6
* sn********@gmail.com:
I have a case where an exception in the constructor of class with a
virtual base leads to termination:

struct vbase {};

struct foo : virtual vbase {
foo() {
throw "exception in foo ctor";
}
};

main()
{
struct bar : public foo {};
try {
bar a;
}
catch ( ... ) {
}
return 0;
};
Typos: result type, semicolon.

This program demonstrates the problem in g++ 3.4. Instead of catching
the exception, the program terminates. The base dtor gets called as
expected, but upon return, there is some generated code that I can't
decipher, which jumps to some termination code at bad_alloc + 80.

That should be a clue... but I'm unable to catch it.


A workaround is

struct bar: foo{ bar(): vbase() {} };

It seems that g++ incorrectly generates a default constructor (or generates
corresponding code, or no code) that doesn't initialize vbase.

Of course, if the actual code has a bunch of interface classes instead of
something like vbase, "initializing" them all is somewhat impractical, but
if that's what's needed, then it's needed. I'd post an error report if I
were you. Just google up the reporting page on the web.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 13 '05 #7
Alf, you had my hopes up, but I don't think this is a fix.

I don't think the absence of base class initialization is the source of
my trouble. In any case,this mode that explicitly calls it still
exhibits the problem.

If I step into the disassembly, I see things proceed as I expect. The
base ctor is called, then foo<1>,then foo<2>. The throw is reached,
then ~foo<1>(), ~vbase(), the exception is allocated, then we go south.

struct vbase {};

template<int N>
struct foo : virtual private vbase {
foo() : vbase() {
if ( N == 2 )
throw "something bad happened";
}
};

struct bar
: public foo<1>
, public foo<2>
, public foo<3>
{};

int main()
{

try { bar a; }
catch ( ... ) {}
return 0;
}

|
| Mark
|

Aug 13 '05 #8
One more point... the compiler correctly only generates a single call
to the virtual base constructor, and it calls it ahead of the
constructors for the instances of foo<N>. In fact, the call to vbase()
added to the foo ctor gets thrown away. It might be a good idea fo the
compiler to generate a warning in this case, if it could properly
figure out when that initializer does not get invoked.

|
| Mark
|

Aug 13 '05 #9
Some progress. In the example above, the problem disappears if you
change the code to:

struct bar
: public foo<1>
, public foo<2>
, public foo<3>
{
bar(){}
};

Yes, just adding a constructor to the base class causes the problem to
go away. I'm checking this on gcc 4.0.0 on an OS X target, I'll double
check with 3.4.

This doesn't completely fix the problem, but I think I can force users
to implement a constructor by creating a non-default ctor for my
virtual base...

I'm going see if the disassembly of the succesful case yields any
additional clues.
|
| Mark
|

Aug 13 '05 #10
* ma***@ieee.org:
Alf, you had my hopes up, but I don't think this is a fix.
Not a completely general one, no, but it is a fix.

This is cool: your new code demonstrates one bug in g++ 3.4.*, and another
bug in MSVC 7.1. :-)

I don't think the absence of base class initialization is the source of
my trouble. In any case,this mode that explicitly calls it still
exhibits the problem.

If I step into the disassembly, I see things proceed as I expect. The
base ctor is called, then foo<1>,then foo<2>. The throw is reached,
then ~foo<1>(), ~vbase(), the exception is allocated, then we go south.

struct vbase {};

template<int N>
struct foo : virtual private vbase {
Make that 'protected' in order to enable the fix.

The MSVC 7.1 compiler bug: it doesn't honor 'private' in this context.

foo() : vbase() {
if ( N == 2 )
throw "something bad happened";
}
};

struct bar
: public foo<1>
, public foo<2>
, public foo<3>
{
bar(): vbase() {} // g++ 3.4 compiler bug workaround.
};


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 13 '05 #11
>Make that 'protected' in order to enable the fix.

That private inheritance was a leftover from some previous
experimentation, my bad.:-)

But you're right about the fix, adding the constructor in the base
class does the job. But it does the job even *without* the base class
initialization, at least in 4.0.0. I think I have enough here to cobble
together a workaround, thanks for providing the line of code that
opened the door!

|
| Mark
|

Aug 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
3
by: Tony Johansson | last post by:
Hello! When you allocate object dynamically which mean on the heap I find that a problem when using exception. What is the prefer method to handle this kind of problem. //Tony
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
16
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
2
by: Chris Stiefeling | last post by:
Hi, I am experiencing a strange problem. I am reading and writing xml files via XmlDocument and XmlTextWriter. In the debugger everything works fine but outside the debugger (debug or release)...
3
by: Robert Rawlins | last post by:
Hi Mk, Yeah it's got me a little bemused to be honest, I've tried playing around with configuration options this morning and not been able to achieve anything that works properly. I'll keep...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.