473,770 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4226
sn********@gmai l.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********@gma il.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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********@gma il.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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********@gmai l.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, "initializi ng" 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

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

Similar topics

6
2136
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 exe file it doesn't catch any errors? Does anyone know what my problem might be? Regards
3
2254
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
2958
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 application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited record layouts, multiple record types per file, one file per chosen client. I have been working on...
16
6592
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 compiler would give you error. In C#, is there a way to do that?
44
4228
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
40
13535
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
5
3437
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
7
2707
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 server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse object for more information. The response returned is shown below. At first I thought this was a...
2
2892
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) I receive the following error: "The type initializer for "System.Xml.Schema.Validator" threw an exception." I wrote a small console app that contains the problem -- I've just attached the default class which gets run. Output outside the...
3
2041
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 testing though and as soon as I have a root cause to the problem I'll be sure to let the list know.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.