This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Run-time error with virtual function w/o invocation
- From: Arthur Schwarz <aschwarz1309 at att dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 30 May 2012 21:39:37 -0700 (PDT)
- Subject: Run-time error with virtual function w/o invocation
- References: <1338438456.44262.YahooMailRC@web180814.mail.gq1.yahoo.com>
I'm using g++ 4.5.3 under cygwin using the Netbeans 7.1.1 IDE. A segment
fault occurs at runtime with the following code.
================== code ==================
#include <iostream>
using namespace std;
class baseClass {
public:
static void* operator new(size_t size) { cout << "new" << endl << flush;};
static void operator delete(void* X) { cout << "delete" << endl <<
flush;}
// virtual void fnc() { cout << "baseClass fnc" << endl << flush; }
protected:
baseClass() { cout << "baseClass constructor" << endl << flush;};
~baseClass() { cout << "baseClass destructor" << endl << flush;};
};
class inheritClass : public baseClass {
protected:
// virtual void fnc() { cout << "inheritClass fnc" << endl << flush; }
public:
inheritClass() { cout << "inherit constructor" << endl << flush;}
~inheritClass() { cout << "inherit destructor" << endl << flush;}
};
int main(int argc, char** argv) {
inheritClass* inherit;
inherit = new inheritClass();
delete inherit;
return 0;
}
=============== end of code ===============
If the virtual function is uncommented then the runtime fails when 'new' is
executed. The thread shows entry to the 'operator new' function and
immediate branching to the nheritClass() constructor and immediate
branching to the 'baseClass()' constructor. When the 'baseClass' constructor
is executed a segmentation fault occurs ("Signal received: SIGSEGV
(Segmentation fault) For program test, pid 8,652". The code works correctly
with the virtual function commented.
I hope that I'm saying things correctly.
After compiling the example program in the cygwin bash shell using
'g++ main.cpp',
with the virtual function uncommented, the following stack dump occurrs.:
Exception: STATUS_ACCESS_VIOLATION at eip=5912356C
eax=00000010 ebx=5915ED14 ecx=5915ED14 edx=00000000 esi=0028ABA8 edi=6119FE9F
ebp=0028AB58 esp=0028AB40 program=C:\home\skidmarks\Projects\Test\Test\a.exe,
pid 8016, thread main
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame Function Args
0028AB58 5912356C (5915ED14, 6119FE9F, 0028AB98, 61102E21)
0028AB78 59123AB6 (0028ABA8, 59160480, BBBAB9B8, 611856C0)
0028ABC8 5914B1AB (59160480, 00402099, 00000015, 59160480)
0028ABE8 5914EB1E (59160480, 00402099, 00000003, 00000000)
0028AC08 00401A1B (59160480, 0040125C, 00000002, 00000000)
0028AC28 004018F2 (59160480, 00000001, 0028FD24, 00000002)
0028AC68 00401198 (00000001, 0028AC90, 80010100, 6127A9A0)
0028ACF8 61007128 (00000000, 0028CD78, 61006720, 00000000)
End of stack trace
Any idea what I can do to fix this thing.