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 » This is heap leak? (a Ptr use new,no delete,heap leak)
icon3.gif  This is heap leak? [message #61786] Sat, 30 August 2025 00:44 Go to next message
fzx374cn is currently offline  fzx374cn
Messages: 1
Registered: August 2025
Junior Member
#include <CtrlLib/CtrlLib.h>

using namespace Upp;
class Test : public TopWindow {
public:
Test();
};

Test::Test()
{
Ptr<Button> ptr;
ptr = new Button;
}

GUI_APP_MAIN
{
Test t;
t.Run();
}
F5 run this code, when exit, popup a window,Is it heap leak?
Re: This is heap leak? [message #61787 is a reply to message #61786] Tue, 02 September 2025 11:26 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1227
Registered: August 2007
Senior Contributor
Hi,

Yes that is a heap leak. You are creating a button on the heap but never deleting it.
Ptr<T> is used for pointing things, not for memory management.
If you want to create ctrls on the heap, you can use containers: One<Buttorn> or Array<Button>.
They can take care of the ownership and track the object life time.

Best regads,
Oblivion


[Updated on: Tue, 02 September 2025 11:27]

Report message to a moderator

Re: This is heap leak? [message #61788 is a reply to message #61787] Tue, 02 September 2025 11:41 Go to previous message
koldo is currently offline  koldo
Messages: 3444
Registered: August 2008
Senior Veteran
Hi Fzx374cn

In addition Ptr goes with Pte.
This is a version of your example. It is not very meaningful but you can see that your pointer does not produce a heap problem, it just returns 0 if the pointed data (Foo data) is out of scope:

struct Foo : Pte<Foo> {
    Button but;
};

class Test : public TopWindow {
public:
	void Start() {
		Foo data;
		ptr = &data;
		str << "During: " << (void*)~ptr << "\n";
	}
	Ptr<Foo> ptr;
	String str;
};

GUI_APP_MAIN
{
	Test t;
	t.str << "Before: " << (void*)~t.ptr << "\n";
	t.Start();
	t.Run();
	t.str << "After:  " << (void*)~t.ptr;
	Exclamation(DeQtfLf(t.str));
}


This shows:
Before: 0x0
During: 0x6fad31f508
After:  0x0


Best regards
Iñaki
Previous Topic: VectorMap iteration
Next Topic: Add compilable testcases for nontrivial problems!
Goto Forum:
  


Current Time: Thu Sep 04 06:28:09 CEST 2025

Total time taken to generate the page: 0.06079 seconds