Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Polymorphic Array doubt
Polymorphic Array doubt [message #33399] Sun, 31 July 2011 23:52 Go to next message
koldo is currently offline  koldo
Messages: 3356
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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Wink

Best regards,
Honza

[Updated on: Mon, 01 August 2011 14:46] by Moderator

Report message to a moderator

Re: Poluporphyc Array doubt [message #33407 is a reply to message #33401] Mon, 01 August 2011 09:53 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
Oups!

Thank you Honza! Smile


Best regards
Iñaki
Re: Poluporphyc Array doubt [message #33409 is a reply to message #33407] Mon, 01 August 2011 11:52 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
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.

Re: Poluporphyc Array doubt [message #33410 is a reply to message #33409] Mon, 01 August 2011 12:50 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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
Re: Poluporphyc Array doubt [message #33413 is a reply to message #33410] Tue, 02 August 2011 10:35 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
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 !
Re: Poluporphyc Array doubt [message #33416 is a reply to message #33413] Wed, 03 August 2011 09:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

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.
Re: Poluporphyc Array doubt [message #33425 is a reply to message #33424] Thu, 04 August 2011 10:30 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
tojocky wrote on Thu, 04 August 2011 11: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.

I say it is a programming error. Without he virtual destructor, the compiler can't free the cal the destructor of the string from StringNumber. It will always call the destructor for type T, in this case Number.
Re: Poluporphyc Array doubt [message #33426 is a reply to message #33425] Thu, 04 August 2011 12:36 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
Yes, that's it. I thought destructors were virtual by default.

Best regards
Iñaki
Re: Poluporphyc Array doubt [message #33427 is a reply to message #33426] Thu, 04 August 2011 12:38 Go to previous message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
koldo wrote on Thu, 04 August 2011 13:36

Yes, that's it. I thought destructors were virtual by default.

Nothing is virtual by default because of the added cost of the vtable. C++ inherits from C, so they wouldn't force you to use something with less performance and higher memory use by default Smile.
Previous Topic: PLATFORM_X11 warnings
Next Topic: BUG? or Not BUG? LoadFile(filename) and then getting wrong data
Goto Forum:
  


Current Time: Tue Apr 16 16:59:08 CEST 2024

Total time taken to generate the page: 0.00393 seconds