public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/3608: Virtual funtions called with wrong this pointer when having virtual inheritance
@ 2001-07-08 6:56 gbr
0 siblings, 0 replies; 2+ messages in thread
From: gbr @ 2001-07-08 6:56 UTC (permalink / raw)
To: gcc-gnats
>Number: 3608
>Category: c++
>Synopsis: Virtual funtions called with wrong this pointer when having virtual inheritance
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sun Jul 08 06:56:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Vladimir Panov
>Release: 2.95.3 20010315 (release)
>Organization:
>Environment:
i386-slackware-linux
>Description:
When invoking a virtual function via a base class pointer the function receives a wrong this pointer. This happens only when having virtual inheritance. I suppose it receives the this pointer for the most-derived object.
This causes no problem (i.e. both this pointers in the ctor and the virtual method are equal), because object b is the most-derived one:
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
public:
A (void)
{
}
virtual ~A (void)
{
}
virtual void t (void)
{
}
};
void T (A *a)
{
a->t ();
}
class B : public virtual A
{
public:
B (void)
{
cerr << "Ctor: " << hex << (unsigned)this << endl;
T (this);
}
virtual ~B (void)
{
}
virtual void t (void)
{
cerr << "t: " << hex << (unsigned)this << endl;
}
};
int main (void)
{
B b;
return 0;
}
This causes no problem, because class C does not have extra members over class B and (C *) is equivalent to (B *):
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
public:
A (void)
{
}
virtual ~A (void)
{
}
virtual void t (void)
{
}
};
void T (A *a)
{
a->t ();
}
class B : public virtual A
{
public:
B (void)
{
cerr << "Ctor: " << hex << (unsigned)this << endl;
T (this);
}
virtual ~B (void)
{
}
virtual void t (void)
{
cerr << "t: " << hex << (unsigned)this << endl;
}
};
class C : public B
{
public:
C (void)
{
}
virtual ~C (void)
{
}
};
int main (void)
{
C c;
return 0;
}
This causes the problem, because (I suppose) the virtual inheritance of class C adds some hidden extra fields:
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
public:
A (void)
{
}
virtual ~A (void)
{
}
virtual void t (void)
{
}
};
void T (A *a)
{
a->t ();
}
class B : public virtual A
{
public:
B (void)
{
cerr << "Ctor: " << hex << (unsigned)this << endl;
T (this);
}
virtual ~B (void)
{
}
virtual void t (void)
{
cerr << "t: " << hex << (unsigned)this << endl;
}
};
class C : public virtual B
{
public:
C (void)
{
}
virtual ~C (void)
{
}
};
int main (void)
{
C c;
return 0;
}
This causes the problem, because class C has extra fields over class B:
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
public:
A (void)
{
}
virtual ~A (void)
{
}
virtual void t (void)
{
}
};
void T (A *a)
{
a->t ();
}
class B : public virtual A
{
public:
B (void)
{
cerr << "Ctor: " << hex << (unsigned)this << endl;
T (this);
}
virtual ~B (void)
{
}
virtual void t (void)
{
cerr << "t: " << hex << (unsigned)this << endl;
}
};
class C : public B
{
int i;
public:
C (void)
{
}
virtual ~C (void)
{
}
};
int main (void)
{
C c;
return 0;
}
Both the erroneous examples are OK with Micro$oft Visual C++ 6.0. Shame on you! :-)))
In the following days I will try to test these examples on Solaris 7 ULTRASPARC with gcc 2.9x.xx to find out if this is an architecture-specific or general bug.
>How-To-Repeat:
g++ example.cpp
and run the resulting a.out.
>Fix:
None.
>Release-Note:
>Audit-Trail:
>Unformatted:
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: c++/3608: Virtual funtions called with wrong this pointer when having virtual inheritance
@ 2001-07-09 13:53 nathan
0 siblings, 0 replies; 2+ messages in thread
From: nathan @ 2001-07-09 13:53 UTC (permalink / raw)
To: gbr, gcc-bugs, gcc-prs, nobody
Synopsis: Virtual funtions called with wrong this pointer when having virtual inheritance
State-Changed-From-To: open->closed
State-Changed-By: nathan
State-Changed-When: Mon Jul 9 13:53:45 2001
State-Changed-Why:
this is fixed in 3.0
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=3608&database=gcc
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-07-09 13:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-08 6:56 c++/3608: Virtual funtions called with wrong this pointer when having virtual inheritance gbr
2001-07-09 13:53 nathan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).