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 » Heap-leaks and polymorphic containers
Heap-leaks and polymorphic containers [message #15857] Mon, 12 May 2008 17:41 Go to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
It is my understanding that the following code should execute without any memory leaks:
struct Item {
	String name;	
};

struct Collection : public Item
{
	Array<Item> items;
};

GUI_APP_MAIN
{
        // Example 1
	Array<Item> array;
	array.Create<Collection>().items.Add();

        // Example 2
	Collection *col = new Collection();
	col->items.Add();
	Item *item = (Item *)col;
	delete item;
}

But for some reason the destructor for 'items' isn't getting called. Adding a virtual destructor to force clearance just causes a crash in MemoryFreeDebug.

Is something broken or am I just missing something obvious?

[Updated on: Mon, 12 May 2008 17:49]

Report message to a moderator

Re: Heap-leaks and polymorphic containers [message #15858 is a reply to message #15857] Mon, 12 May 2008 19:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mrjt wrote on Mon, 12 May 2008 11:41

It is my understanding that the following code should execute without any memory leaks:
struct Item {
	String name;	
};

struct Collection : public Item
{
	Array<Item> items;
};

GUI_APP_MAIN
{
        // Example 1
	Array<Item> array;
	array.Create<Collection>().items.Add();

        // Example 2
	Collection *col = new Collection();
	col->items.Add();
	Item *item = (Item *)col;
	delete item;
}

But for some reason the destructor for 'items' isn't getting called. Adding a virtual destructor to force clearance just causes a crash in MemoryFreeDebug.

Is something broken or am I just missing something obvious?


Well, virtual destructor is absolutely required here.

The crash in MemoryFreeDebug... well, who knows, I would say it has another reason.

Mirek
Re: Heap-leaks and polymorphic containers [message #15864 is a reply to message #15858] Tue, 13 May 2008 15:46 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
luzr wrote on Mon, 12 May 2008 18:05

Well, virtual destructor is absolutely required here.
Hmm. I understand why this works (the implicit destructor isn't virtual):
struct Item : public Moveable<Item> {
	String name;
	
	virtual ~Item() { }	
};

struct Collection : public Item
{
	Array<Item> items;
};

GUI_APP_MAIN
{
	Collection *col = new Collection();
	col->items.Add();
	Item *item = (Item *)col;
	delete item;
}

But what baffles me is why this works with Vector, but not Array (the Array's destructor doesn't get called but the Vector one does).
struct Item : public Moveable<Item> {
	String name;	
};

struct Collection : public Item
{
	Vector<Item> items; // Heap leak if changed to Array
};

GUI_APP_MAIN
{
	Collection *col = new Collection();
	col->items.Add();
	Item *item = (Item *)col;
	delete item;
}
Confused

[Updated on: Tue, 13 May 2008 15:59]

Report message to a moderator

Re: Heap-leaks and polymorphic containers [message #15865 is a reply to message #15864] Tue, 13 May 2008 16:00 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Arrrgh, a bug in leak detector...

Vector is using "MemoryAllocSz" variant, which is not implement with leak checking.

Mirek
Re: Heap-leaks and polymorphic containers [message #15866 is a reply to message #15865] Tue, 13 May 2008 16:02 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Thank god! I was beginning to think I was delusional Smile

[Updated on: Tue, 13 May 2008 16:03]

Report message to a moderator

Re: Heap-leaks and polymorphic containers [message #15878 is a reply to message #15865] Tue, 13 May 2008 23:39 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
luzr wrote on Tue, 13 May 2008 16:00

Arrrgh, a bug in leak detector...

Vector is using "MemoryAllocSz" variant, which is not implement with leak checking.

Mirek


That one drove me crazy when I was looking for memory violation bug ! Sad

Max
Re: Heap-leaks and polymorphic containers [message #15883 is a reply to message #15878] Wed, 14 May 2008 08:30 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mdelfede wrote on Tue, 13 May 2008 17:39

luzr wrote on Tue, 13 May 2008 16:00

Arrrgh, a bug in leak detector...

Vector is using "MemoryAllocSz" variant, which is not implement with leak checking.

Mirek


That one drove me crazy when I was looking for memory violation bug ! Sad

Max



Yeah, a time to fix this.

Mirek
Previous Topic: Using NTL outside of U++?
Next Topic: System() call
Goto Forum:
  


Current Time: Tue Apr 23 10:59:56 CEST 2024

Total time taken to generate the page: 0.02290 seconds