Home » U++ Library support » U++ Core » Polymorphic Array doubt
Polymorphic Array doubt [message #33399] |
Sun, 31 July 2011 23:52 |
|
koldo
Messages: 3361 Registered: August 2008
|
Senior Veteran |
|
|
Hello all
I have a doubt with polymorphic Arrays. In my cases methods are called properly but the destructor called is the one of the base class.
For example:
struct Number {
virtual double Get() const = 0;
};
struct IntegerNumber : public Number {
int n;
virtual double Get() const { return n; }
IntegerNumber(int n) : n(n) {}
};
struct StringNumber : public Number {
String n;
virtual double Get() const { return atof(n); }
StringNumber(String n) : n(n) {}
};
If you include this in your code:
Array<Number> num;
num.Add(new IntegerNumber(3));
num.Add(new StringNumber("15.555555555555555555555555555555555555"));
you will get a "HEAP LEAKS" error when deletion, as for all Array members the Number destructor is called, instead of StringNumber or IntegerNumber.
Best regards
Iñaki
[Updated on: Mon, 01 August 2011 14:45] Report message to a moderator
|
|
|
Re: Polymorphic Array doubt [message #33401 is a reply to message #33399] |
Mon, 01 August 2011 07:46 |
|
Hi Koldo,
You need to provide a virtual destructor in your base class, so that the compiler knows it should look in the derived classes as well:struct Number {
virtual double Get() const = 0;
virtual ~Number(){}
};
This will cause both ~Number() and ~{Integer,String}Number() to be called upon deletion. Which in turn removes the leak
Best regards,
Honza
[Updated on: Mon, 01 August 2011 14:46] by Moderator Report message to a moderator
|
|
|
|
|
|
|
Re: Poluporphyc Array doubt [message #33416 is a reply to message #33413] |
Wed, 03 August 2011 09:39 |
|
mirek
Messages: 13976 Registered: November 2005
|
Ultimate Member |
|
|
Didier wrote on Tue, 02 August 2011 04:35 |
mirek wrote on Mon, 01 August 2011 12:50 |
Didier wrote on Mon, 01 August 2011 05:52 | Hi Koldo and Honza,
I noticed that the virual destructor was sometimes missing in some controls, ex: RichEditWithToolBar
Fortunatly this does not lead to any leaks (most of the time) thank's to the "everything belongs somewhere" in U++.
But this is a real bug.
|
'virtuality' of destructor is inherited in C++. Therefore, as long as Ctrl has virtual destructor (and it does...), all derived classes have one too.
Mirek
|
Humm,
I remember some work I did 10 years ago and this was not true then, but if gcc deals with it now : great !
|
Well, this is so basic feature that if it would not work, compiler would be seriously broken...
|
|
|
Re: Poluporphyc Array doubt [message #33424 is a reply to message #33416] |
Thu, 04 August 2011 10:21 |
|
Hello,
If in the base class is not set virtual destructor, and detect memory leak in example of Koldo, is this a bug?
In my case on win32 and linux 32 (ubuntu 11.04) have the same behavor (memory leak).
Regards,
Ion.
|
|
|
|
|
|
Goto Forum:
Current Time: Fri May 10 01:38:46 CEST 2024
Total time taken to generate the page: 0.02433 seconds
|