[Bug c++/69874] New: Program crashes when an exception is thrown to second base class reference
maysam.kind at gmail dot com
gcc-bugzilla@gcc.gnu.org
Fri Feb 19 14:54:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69874
Bug ID: 69874
Summary: Program crashes when an exception is thrown to second
base class reference
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: blocker
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: maysam.kind at gmail dot com
Target Milestone: ---
I tried the following code using several GCCs and got problems when I run the
code which is compiled using O1 with gcc4.7.1 and onward.
#include <stdexcept>
#include <iostream>
struct Interface_1
{
virtual void setA() = 0;
};
struct Interface_2
{
virtual void setB() = 0;
};
struct Master: Interface_1, Interface_2
{
void setA()
{
throw std::runtime_error("I was thrown");
}
void setB()
{
throw std::runtime_error("I was thrown");
}
};
int main()
{
Master m;
Interface_2& i2 = m;
try
{
i2.setB();
}
catch (const std::runtime_error& e)
{
std::cout << "Caught exception e=" << e.what() << std::endl;
}
catch (...)
{
std::cout << "Did not catch exception" << std::endl;
}
std::cout << "Done running" << std::endl;
}
In the code above I am a reference from interface Interface_2 to refer to an
object from Master class, and this will result into a crash
terminate called after throwing an instance of 'std::runtime_error'
what(): I was thrown
Abort (core dumped)
But it works if I use a reference from Interface_1:
int main()
{
Master m;
Interface_1& i1 = m;
try
{
i1.setA();
}
catch (const Exception& e)
{
std::cout << "Caught exception e=" << e.what() << std::endl;
}
catch (...)
{
std::cout << "Did not catchexception" << std::endl;
}
std::cout << "Done running" << std::endl;
}
I just get what is expected:
Caught exception e=I was thrown
Done running
It seems that the first interface that I inherit from can just be used to refer
to a object from Master class. I also made the inheritance from the first
interface as follows:
class Master: public virtual Interface_1, public Interface_2
Then references from Interface_2 just turned out to be fine. Instead, I got
problem with references from Interface_1.
More information about the Gcc-bugs
mailing list