Calling virtual override from base function fails

D

David

Can anyone tell me why the following code crashes? The pure base
class StaticObjectRegistry stores pointers to derived classes upon
construction. Later it uses these pointers to call the overridden
function ClearStaticData() through its base function Handler(). That
is where the code crashes. (I'm using VC++6.0)


#include <iostream>
#include <list>

using namespace std;

class StaticObjectRegistry
{
public:
static list<StaticObjectRegistry*> objects;
StaticObjectRegistry() {
static bool registered;
if(!registered) {
objects.push_back(this);
registered = true;
}
};
static void ResetStaticObjects(void);
void Handler(void) {
ClearStaticData();
}
virtual void ClearStaticData(void) = 0;
};

list<StaticObjectRegistry*> StaticObjectRegistry::eek:bjects;

void StaticObjectRegistry::ResetStaticObjects(void) {
for(list<StaticObjectRegistry*>::iterator i = objects.begin(); i !=
objects.end(); i++)
(*i)->Handler();
}

class P : public StaticObjectRegistry {
public:
void ClearStaticData(void) {cout << "In p.clear" << endl; c = 0;}
static int c;
P() {c++; cout << "P: " << c << endl;};
};

int P::c;

void main()
{
P p;
p.ResetStaticObjects();
}
 
J

jones

David said:
Can anyone tell me why the following code crashes? The pure base
class StaticObjectRegistry stores pointers to derived classes upon
construction. Later it uses these pointers to call the overridden
function ClearStaticData() through its base function Handler(). That
is where the code crashes. (I'm using VC++6.0)

What do you mean "it crashes" ? does it segfault? i've tried it with gcc
3.3.2 and it works perfectly, of course making "main" return int and
not void.
 
D

David

Oops ... looks like I oversimplified. To get the crash add the
following code:

void Sub(void) {P test;}

void main()
{
Sub();
P p;
p.ResetStaticObjects();
}

.... which makes it clear that I am trying to do something a little
more difficult. Here, the "test" instance is registered and then
destroyed but the StaticObjectRegistry doesn't remove the pointer to
it, and doesn't add a pointer to the new instance in main(). But
correctly creating and destroying pointers to every object instance
doesn't meet my goals.


What I wanted was to be able to reset static data on multiple classes
EVEN IF THERE ARE NO CURRENT INSTANCES. Here's a version that
accomplishes this. I guess I'm answering my own question here, but
maybe others will find this StaticObjectRegistry to be useful:

#include <algorithm>
#include <iostream>
#include <list>

using namespace std;

class StaticObjectRegistry
{
public:
typedef void (*ResetFunction)();
static void ResetStaticObjects(void);

StaticObjectRegistry(ResetFunction RF) {
if(find(ResetObjects.begin(), ResetObjects.end(), RF) ==
ResetObjects.end())
ResetObjects.push_back(RF);
};
private:
StaticObjectRegistry();
static list<ResetFunction> ResetObjects;
};

void StaticObjectRegistry::ResetStaticObjects(void) {
for(list<ResetFunction>::iterator i = ResetObjects.begin(); i !=
ResetObjects.end(); i++)
(**i)();
}

list<StaticObjectRegistry::ResetFunction>
StaticObjectRegistry::ResetObjects;

// BEGIN SAMPLE USAGE
class P : public StaticObjectRegistry {
public:
static void ClearStaticData(void) {cout << "In p.clear" << endl;
c = 0;};
static int c;
P() : StaticObjectRegistry(ClearStaticData) {c++; cout << "P: "
<< c << endl;};
};

int P::c;

void Sub(void) {
P test;
}

void main()
{
Sub();
P::ResetStaticObjects();
P p;
p.ResetStaticObjects();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top