public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* This C++ code fails
@ 1999-03-15  3:05 Ian Farquharson
  1999-03-31 19:45 ` Ian Farquharson
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Farquharson @ 1999-03-15  3:05 UTC (permalink / raw)
  To: cygwin

This small test program demonstrates a problem I am having with virtual base
classes and a pointer to member.

When I try to call a function in a class that has virtual base classes
through a pointer to member the program crashes.

I have compiled and run this same test successfully under Borland C++ 5.02

I am using cygwin B20.1 with EGCS 1.1.1 installed. You can compile the test
using the command "gcc Try.cpp -o Try.exe". When the program is run I get
the following output:

bash-2.02$ gcc try.cpp -o try.exe
bash-2.02$ ./try
Calling Test1::Click directly.
void Test1::Click(void)
Calling Test1::Click through pointer to member.
void Test1::Click(void)
Calling Test2::Click directly.
void Test2::Click(void)
Calling Test2::Click through pointer to member.
[main] D:\race\test\try.exe 1016 (0) handle_exceptions: Exception:
STATUS_ACCESS
_VIOLATION
[main] try 1016 (0) handle_exceptions: Dumping stack trace to try.exe.core

The Test2 class is using virtual base classes, unlike the Test1 class. This
difference causes an exception to be thrown when the pointer to member is
used.

Any help would be much appreciated.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* This C++ code fails
  1999-03-15  3:05 This C++ code fails Ian Farquharson
@ 1999-03-31 19:45 ` Ian Farquharson
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Farquharson @ 1999-03-31 19:45 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1123 bytes --]

This small test program demonstrates a problem I am having with virtual base
classes and a pointer to member.

When I try to call a function in a class that has virtual base classes
through a pointer to member the program crashes.

I have compiled and run this same test successfully under Borland C++ 5.02

I am using cygwin B20.1 with EGCS 1.1.1 installed. You can compile the test
using the command "gcc Try.cpp -o Try.exe". When the program is run I get
the following output:

bash-2.02$ gcc try.cpp -o try.exe
bash-2.02$ ./try
Calling Test1::Click directly.
void Test1::Click(void)
Calling Test1::Click through pointer to member.
void Test1::Click(void)
Calling Test2::Click directly.
void Test2::Click(void)
Calling Test2::Click through pointer to member.
[main] D:\race\test\try.exe 1016 (0) handle_exceptions: Exception:
STATUS_ACCESS
_VIOLATION
[main] try 1016 (0) handle_exceptions: Dumping stack trace to try.exe.core

The Test2 class is using virtual base classes, unlike the Test1 class. This
difference causes an exception to be thrown when the pointer to member is
used.

Any help would be much appreciated.

[-- Attachment #2: Try.cpp --]
[-- Type: text/x-c++, Size: 2371 bytes --]

/****************************************************************************\
*                                                                            *
* Test program that demonstrates bug with virtual base classes and pointer   *
* to member                                                                  *
*                                                                            *
* Author: Ian Farquharson (ifarquha@ihug.co.nz)                              *
*                                                                            *
\****************************************************************************/

#include <stdlib.h>
#include <stdio.h>

// Class declarations ========================================================

class Base1
{
public:
  virtual ~Base1(void);
};

class Base2
{
public:
  virtual ~Base2(void);
  virtual void Click(void);
};

class Test1: public Base1, public Base2                 // NB: non-virtual
{
public:
  virtual ~Test1(void);
  virtual void Click(void);
};

class Test2: virtual public Base1, virtual public Base2 // NB: virtual
{
public:
  virtual ~Test2(void);
  virtual void Click(void);
};

// Class implementation ======================================================

Base1::~Base1(void)
{
}

Base2::~Base2(void)
{
}

void Base2::Click(void)
{
  printf("void Base2::Click(void)\n");
}

Test1::~Test1(void)
{
}

void Test1::Click(void)
{
  printf("void Test1::Click(void)\n");
}

Test2::~Test2(void)
{
}

void Test2::Click(void)
{
  printf("void Test2::Click(void)\n");
}

// RunTest1 demonstrates a class using multiple inheritence ==================

typedef void (Test1::*Test1Proc)(void);

void RunTest1(void)
{
  Test1 test;

  printf("Calling Test1::Click directly.\n");
  test.Click();

  printf("Calling Test1::Click through pointer to member.\n");
  Test1Proc proc = &Test1::Click;
  (test.*proc)();
}

// RunTest2 demonstrates a class using multiple virtual inheritence ==========

typedef void (Test2::*Test2Proc)(void);

void RunTest2(void)
{
  Test2 test;

  printf("Calling Test2::Click directly.\n");
  test.Click();

  printf("Calling Test2::Click through pointer to member.\n");
  Test2Proc proc = &Test2::Click;
  (test.*proc)();
}

// main ======================================================================

int main(int, char* [])
{
  RunTest1();
  RunTest2();
  return 0;
}



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: This C++ code fails
  1999-03-15  8:18 Nirmal Prasad
@ 1999-03-31 19:45 ` Nirmal Prasad
  0 siblings, 0 replies; 4+ messages in thread
From: Nirmal Prasad @ 1999-03-31 19:45 UTC (permalink / raw)
  To: cygwin; +Cc: ifarquha

>The Test2 class is using virtual base classes, unlike the Test1 class. This
>difference causes an exception to be thrown when the pointer to member
isused.

hi there,

well i havent taken a serious look at it .. but here's what i did and the
results are interesting ......
Change the declaration for Test2 as follows:-
class Test2: virtual public Base2
i.e. drop Virtual public Base 1 and let the remainder of the program be the
same... The CPU usage JUMPS TO 100% and the program keeps leaking memory.. i
checked the disassembly in gdb and there is a jmp that keeps happening at
that point ... Anyway probably i'll take a serious look at it later....
Something going wrong i guess somewhere in codegen.

Regards

Nirmal Prasad R.


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: This C++ code fails
@ 1999-03-15  8:18 Nirmal Prasad
  1999-03-31 19:45 ` Nirmal Prasad
  0 siblings, 1 reply; 4+ messages in thread
From: Nirmal Prasad @ 1999-03-15  8:18 UTC (permalink / raw)
  To: cygwin; +Cc: ifarquha

>The Test2 class is using virtual base classes, unlike the Test1 class. This
>difference causes an exception to be thrown when the pointer to member
isused.

hi there,

well i havent taken a serious look at it .. but here's what i did and the
results are interesting ......
Change the declaration for Test2 as follows:-
class Test2: virtual public Base2
i.e. drop Virtual public Base 1 and let the remainder of the program be the
same... The CPU usage JUMPS TO 100% and the program keeps leaking memory.. i
checked the disassembly in gdb and there is a jmp that keeps happening at
that point ... Anyway probably i'll take a serious look at it later....
Something going wrong i guess somewhere in codegen.

Regards

Nirmal Prasad R.


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-03-31 19:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-15  3:05 This C++ code fails Ian Farquharson
1999-03-31 19:45 ` Ian Farquharson
1999-03-15  8:18 Nirmal Prasad
1999-03-31 19:45 ` Nirmal Prasad

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).