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 8 2849
"Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message news:be*************@news.t-online.com... 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?
"Ron Natalie" <ro*@sensor.com> wrote... "Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message
news:be*************@news.t-online.com... 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;
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
"Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message news:be*************@news.t-online.com... Victor 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.
Victor Bazarov wrote: "Ron Natalie" <ro*@sensor.com> wrote... "Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message
news:be*************@news.t-online.com... > 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;
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
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
"Gerhard Wolfstieg" <Wo*******@t-online.de> wrote... Victor Bazarov wrote:
"Ron Natalie" <ro*@sensor.com> wrote... "Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message
news:be*************@news.t-online.com... > 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;
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 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
"Gerhard Wolfstieg" <Wo*******@t-online.de> wrote... 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
"Gerhard Wolfstieg" <Wo*******@t-online.de> wrote in message news:be*************@news.t-online.com... { 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.
>> ... 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.
No. it was a matter of a memory lack in my lexical knowledge about the
behaviour of the Windows ApI. By using the heuristics of writing down (postings) you helped me -- indirectly. So I found the error after a long time now.
Victor
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: nenad |
last post by:
Wouldn't it be nice if we could do something like this:
class Funky{
public: auto virtual void doStuff(){
// dostuff
}
};
|
by: qazmlp |
last post by:
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?
class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n";...
|
by: JKop |
last post by:
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that...
|
by: tuvok |
last post by:
Is it correct that the virtual dtor of base gets called implicitly?
Here's some code to demonstrate what I mean:
Class B has a virtual destructor, so has class D which
is derived from B. Deleting...
|
by: Bruno van Dooren |
last post by:
Hi all,
i am having a problems with inheritance.
consider the following:
class A
{
public:
A(int i){;}
};
|
by: Asfand Yar Qazi |
last post by:
Hi,
I have the following header file in my 'everything useful I think of
in one place' library:
============= BEGIN CODE SNIPPET ===========
/** All classes that derive from this obtain a...
|
by: Imre |
last post by:
Hi!
I've got some questions regarding heavy use of virtual inheritance.
First, let's see a theoretical situation, where I might feel tempted to
use a lot of virtual inheritance.
Let's...
|
by: Dave Rahardja |
last post by:
Since C++ is missing the "interface" concept present in Java, I've been using
the following pattern to simulate its behavior:
class Interface0
{
public:
virtual void fn0() = 0;
};
|
by: =?Utf-8?B?Zmplcm9uaW1v?= |
last post by:
Hi all,
As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in
microsoft.public.windbg), we created a powerful symbol engine using dbghelp
to dump the contents of the stack...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |