virtual dtors and multi inheritance

  • Thread starter Gerhard Wolfstieg
  • Start date
G

Gerhard Wolfstieg

The following situation:

class A0
{ static A0 *a0; // something like this to publish the pointer

public:
A0()
{ a0 = this;
}
virtual ~A0(){}
};

A0::a0;

class A : public A0 { public: virtual ~A(){} };
class B { public: virtual ~B(){} };

class XYZ : public A, public B { public: virtual ~XYZ(){} };

int
main( int, char ** )
{ XYZ *xyz = new XYZ();
// ...
delete A0::a0;
}

I expected to have deleted xyz at the end of main. It does not and that is
the problem. What can I expect following the standard or what have I to do
to have called the xyz dtor?
I cannot find any hints in my books.

Thank you, Gerhard Wolfsieg
 
R

Ron Natalie

Gerhard Wolfstieg said:
I expected to have deleted xyz at the end of main. It does not and that is
the problem. What can I expect following the standard or what have I to do
to have called the xyz dtor?
I cannot find any hints in my books.

First off your program has a couple of problems. The definition of A0::a0
should be:
A0 A0::a0;

and if you want to get at it from main, you'd better make it public.

But notwithstanding those changes, the delete of a0 at the end of main
should properly destroy the value allocated in the first line of main.

What evidence do you have to the contrary?
 
V

Victor Bazarov

Ron Natalie said:
First off your program has a couple of problems. The definition of A0::a0
should be:
A0 A0::a0;

Nah... It should be

A0* A0::a0;

(a pointer to A0)
and if you want to get at it from main, you'd better make it public.

But notwithstanding those changes, the delete of a0 at the end of main
should properly destroy the value allocated in the first line of main.

What evidence do you have to the contrary?

I think Gerhard didn't post the real code, so it's probably something
else, like a multiple inheritance from A0 as well. In the posted code
'B' didn't inherit from A0, whereas in his real code it might...

Victor
 
R

Ron Natalie

Gerhard Wolfstieg said:
I think that I can call the virtual dtor of a base class to delete the
children. And therefore I used the pointer (in some other way) to the base
class object.
What you had better be very careful of is that no other object of type A0 (or
something that inherits from it) is ever created, or else you'll write to a0 multiple
times.
 
G

Gerhard Wolfstieg

Victor said:
Nah... It should be

A0* A0::a0;

(a pointer to A0)


I think Gerhard didn't post the real code, so it's probably something
else, like a multiple inheritance from A0 as well. In the posted code
'B' didn't inherit from A0, whereas in his real code it might...

Victor
Surely, it is not the real Code and therefore the quick an dirty mistake
(A0* A0::a0; is right) But the structure of the classes is the exactly the
same as in the original -- therefore I did not "hide" A0 here.
The main thing is that the object xyz points to is not destroyed, not
stopped at breakpoint, not freed some memory inside the dtor. What has to
be the behaviour of virtual dtors in cases of multiple inheritance like
that one. Is it my code or the compiler (not to exclude strange results by
setting compilerflags) or a third.

Grüße, Gerhard
 
V

Victor Bazarov

Gerhard Wolfstieg said:
Surely, it is not the real Code and therefore the quick an dirty mistake
(A0* A0::a0; is right) But the structure of the classes is the exactly the
same as in the original -- therefore I did not "hide" A0 here.
The main thing is that the object xyz points to is not destroyed, not
stopped at breakpoint, not freed some memory inside the dtor. What has to
be the behaviour of virtual dtors in cases of multiple inheritance like
that one. Is it my code or the compiler (not to exclude strange results by
setting compilerflags) or a third.

Grüße, Gerhard

I don't know what you're talking about.
-------------------------------------------------------
#include <iostream>
using namespace std;

struct A
{
static A* a;
A() { a = this; }
virtual ~A() {}
};

A* A::a;

struct AA : A {};

struct B
{
virtual ~B() {}
};

struct C : AA, B
{
~C() { cout << "C::~C()\n"; }
};

int main()
{
C *pc = new C;

delete A::a;

return 0;
}
----------------------------------------- output:
C::~C()


Victor
 
V

Victor Bazarov

Gerhard Wolfstieg said:
Ok, the situation was:

class BaseWin
{ static BaseWin *basewin;

public:
MVCaseWin()
{ basewin; = this;

It couldn't be. This code doesn't compile!
}
virtual ~MVCaseWin(){}
};

BaseWin *BaseWin::*basewin;;

Again, this is nonsense. This is not the code you had.
You just couldn't have this code. You must have had some
other code that you don't want to show.

Never mind. It doesn't matter now, anyway.
class Win : public BaseWin { public: virtual ~Win(){} };
class MVC { public: virtual ~MVC(){} };

class View : public Win, public MVC { public: virtual ~View(){} };

int
main( int, char ** )
{ new View();
// ...
delete BaseWin::basewin;
}

I think that I have described all quite clearly.
The whole thing works very fine in many binaries -- but one. By complaining
about it I could not detect an error. So I guessed that there could have
been something wrong with my understanding of the mechanism of calling
virtual dtors in complicated situations using multiple inheritance.
The bug was: in one special case the mechanism of having a "delete
Pointer_to_BaseWin" by the OS failed.

The most probably cause for that is that the pointer you were
trying to delete was from another process' memory space.
By using the heuristics of writing down (postings) you helped me --
indirectly. So I found the error after a long time now.

Victor
 
R

Ron Natalie

Gerhard Wolfstieg said:
{ basewin; = this;

Errant semicolon.
BaseWin *BaseWin::*basewin;;

Errant * and semicolon.
The bug was: in one special case the mechanism of having a "delete
Pointer_to_BaseWin" by the OS failed.
By using the heuristics of writing down (postings) you helped me --
indirectly. So I found the error after a long time now.

I still have no clue what you mean by the above.
 
G

Gerhard Wolfstieg

...
The most probably cause for that is that the pointer you were
trying to delete was from another process' memory space.
No. it was a matter of a memory lack in my lexical knowledge about the
behaviour of the Windows ApI.
Thank you for your interest. Please excuse the many bad typings, I did not
copy/paste the code, I just hacked it (too) quickly into the posting and
did not check the replacing of B with MVC (I wanted to give more info, but
made it worse). I just moved to another OS and still the selected small
fonts are ... not optimized.

Gerhard Wolfstieg
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top