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 » Community » Newbie corner » CTRL + C = 659 Heap leaks
Re: CTRL + C = 659 Heap leaks [message #52068 is a reply to message #52067] Mon, 15 July 2019 08:17 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Mon, 15 July 2019 06:15

This is not a correct behavior.


There is no correct behaviour. This is not defined anywhere in c++ standard.

Mirek
Re: CTRL + C = 659 Heap leaks [message #52069 is a reply to message #52068] Mon, 15 July 2019 17:16 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Mon, 15 July 2019 02:17
Novo wrote on Mon, 15 July 2019 06:15

This is not a correct behavior.


There is no correct behaviour. This is not defined anywhere in c++ standard.

Mirek

In this case this is a bug with U++. MemDiagCls shouldn't be a global object. It should be created on stack (by GUI_APP_MAIN or by CONSOLE_APP_MAIN). IMHO.


Regards,
Novo
Re: CTRL + C = 659 Heap leaks [message #52070 is a reply to message #52069] Mon, 15 July 2019 19:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Mon, 15 July 2019 17:16
mirek wrote on Mon, 15 July 2019 02:17
Novo wrote on Mon, 15 July 2019 06:15

This is not a correct behavior.


There is no correct behaviour. This is not defined anywhere in c++ standard.

Mirek

In this case this is a bug with U++. MemDiagCls shouldn't be a global object. It should be created on stack (by GUI_APP_MAIN or by CONSOLE_APP_MAIN). IMHO.


Then it would not work. Think about all that memory allocated in global objects... (and worse, not in initialization phase).
Re: CTRL + C = 659 Heap leaks [message #52071 is a reply to message #52070] Mon, 15 July 2019 20:18 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Mon, 15 July 2019 13:35
Novo wrote on Mon, 15 July 2019 17:16
mirek wrote on Mon, 15 July 2019 02:17
Novo wrote on Mon, 15 July 2019 06:15

This is not a correct behavior.


There is no correct behaviour. This is not defined anywhere in c++ standard.

Mirek

In this case this is a bug with U++. MemDiagCls shouldn't be a global object. It should be created on stack (by GUI_APP_MAIN or by CONSOLE_APP_MAIN). IMHO.


Then it would not work. Think about all that memory allocated in global objects... (and worse, not in initialization phase).

Instead of MemDiagCls you can put on stack a guard-object, which will detect that its destructor got called and set a flag on MemDiagCls.
IMHO, the problem is fixable ...


Regards,
Novo
Re: CTRL + C = 659 Heap leaks [message #52072 is a reply to message #52071] Tue, 16 July 2019 00:11 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Mon, 15 July 2019 20:18
mirek wrote on Mon, 15 July 2019 13:35
Novo wrote on Mon, 15 July 2019 17:16
mirek wrote on Mon, 15 July 2019 02:17
Novo wrote on Mon, 15 July 2019 06:15

This is not a correct behavior.


There is no correct behaviour. This is not defined anywhere in c++ standard.

Mirek

In this case this is a bug with U++. MemDiagCls shouldn't be a global object. It should be created on stack (by GUI_APP_MAIN or by CONSOLE_APP_MAIN). IMHO.


Then it would not work. Think about all that memory allocated in global objects... (and worse, not in initialization phase).

Instead of MemDiagCls you can put on stack a guard-object, which will detect that its destructor got called and set a flag on MemDiagCls.
IMHO, the problem is fixable ...


Yeah, but then I will not catch leaks in global objects.

I could install Ctrl+C handler and do the same trick (disable leaks checker), but is it worth the effort?

Mirek
Re: CTRL + C = 659 Heap leaks [message #52073 is a reply to message #52072] Tue, 16 July 2019 02:11 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Mon, 15 July 2019 18:11

Yeah, but then I will not catch leaks in global objects.

I do not understand why. Just do not show memory leak report when an object on stack wasn't destroyed.


Regards,
Novo
Re: CTRL + C = 659 Heap leaks [message #52076 is a reply to message #52073] Tue, 16 July 2019 09:38 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Tue, 16 July 2019 02:11
mirek wrote on Mon, 15 July 2019 18:11

Yeah, but then I will not catch leaks in global objects.

I do not understand why. Just do not show memory leak report when an object on stack wasn't destroyed.


Maybe we are nor speaking about the same thing...

struct Foo {
   byte *ptr;

   ~Foo() { if(ptr) delete[] ptr; } // this might be missing
};

Foo foo;

CONSOLE_APP_MAIN {
   foo.ptr = new byte[1000];
}


If you couple leaks detection to stack, either that leak is wrongly ignored or not leak wrongly reported (depending on what exactly you do).

Really the only sensible thing to do is either somehow make Ctrl+C call Exit by installing exception handler in Win32, but that is hard as the handler is running in different thread, or make it disable leak checks completely in Win32 in Ctrl+C exception handler.
Re: CTRL + C = 659 Heap leaks [message #52087 is a reply to message #52076] Wed, 17 July 2019 18:44 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Tue, 16 July 2019 03:38

Maybe we are nor speaking about the same thing...

What I mean is

struct MemDiagCls {
	MemDiagCls();
	~MemDiagCls();
	
	static void ShowReport() { show = true; }
	static bool IsShowReport() { return show; }
	
private:
	static bool show;
};
bool MemDiagCls::show = false;

MemDiagCls::~MemDiagCls()
{
	if(--sMemDiagInitCount == 0)
		if (IsShowReport())
			UPP::MemoryDumpLeaks();
}

struct LeakRepGuard {
	~LeakRepGuard();
};

LeakRepGuard::~LeakRepGuard() {
	MemDiagCls::ShowReport();
}

#define CONSOLE_APP_MAIN \
void ConsoleMainFn_(); \
 \
int main(int argc, char *argv[]) { \
	UPP::LeakRepGuard __; \
	UPP::AppInit__(argc, (const char **)argv); \
	UPP::AppExecute__(ConsoleMainFn_); \
	UPP::AppExit__(); \
	return UPP::GetExitCode(); \
} \
 \
void ConsoleMainFn_()

A call to UPP::MemoryDumpLeaks() will be disabled if there was no stack unwinding. In all other cases it will work as before.
Leak report makes no sense when destructors of objects on stack were not called.
IMHO, this should be thread-safe because there is only one main() function ...


Regards,
Novo
Re: CTRL + C = 659 Heap leaks [message #52090 is a reply to message #52087] Wed, 17 July 2019 20:25 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Wed, 17 July 2019 18:44
mirek wrote on Tue, 16 July 2019 03:38

Maybe we are nor speaking about the same thing...

What I mean is

struct MemDiagCls {
	MemDiagCls();
	~MemDiagCls();
	
	static void ShowReport() { show = true; }
	static bool IsShowReport() { return show; }
	
private:
	static bool show;
};
bool MemDiagCls::show = false;

MemDiagCls::~MemDiagCls()
{
	if(--sMemDiagInitCount == 0)
		if (IsShowReport())
			UPP::MemoryDumpLeaks();
}

struct LeakRepGuard {
	~LeakRepGuard();
};

LeakRepGuard::~LeakRepGuard() {
	MemDiagCls::ShowReport();
}

#define CONSOLE_APP_MAIN \
void ConsoleMainFn_(); \
 \
int main(int argc, char *argv[]) { \
	UPP::LeakRepGuard __; \
	UPP::AppInit__(argc, (const char **)argv); \
	UPP::AppExecute__(ConsoleMainFn_); \
	UPP::AppExit__(); \
	return UPP::GetExitCode(); \
} \
 \
void ConsoleMainFn_()

A call to UPP::MemoryDumpLeaks() will be disabled if there was no stack unwinding. In all other cases it will work as before.
Leak report makes no sense when destructors of objects on stack were not called.
IMHO, this should be thread-safe because there is only one main() function ...


Ah, I see, in other words: "or make it disable leak checks completely in Win32 in Ctrl+C exception handler." (your method is more generic but the effect is the same).
Re: CTRL + C = 659 Heap leaks [message #52091 is a reply to message #52087] Wed, 17 July 2019 23:04 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Wed, 17 July 2019 18:44

Leak report makes no sense when destructors of objects on stack were not called.


This is debatable. It would warn you about calling "exit(0)" or equivalent, which is IMO good.
Re: CTRL + C = 659 Heap leaks [message #52092 is a reply to message #52091] Wed, 17 July 2019 23:46 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Wed, 17 July 2019 17:04
Novo wrote on Wed, 17 July 2019 18:44

Leak report makes no sense when destructors of objects on stack were not called.


This is debatable. It would warn you about calling "exit(0)" or equivalent, which is IMO good.

This leak report is very confusing. This thread is a proof of that.
You can do something like this instead ...
MemDiagCls::~MemDiagCls()
{
	if(--sMemDiagInitCount == 0)
		if (IsShowReport())
			UPP::MemoryDumpLeaks();
		else
			UPP::Cerr() << "The World is going to end soon!" << UPP::EOL;
}


Regards,
Novo
Re: CTRL + C = 659 Heap leaks [message #52132 is a reply to message #52067] Tue, 23 July 2019 09:36 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK:

* c:\xxx\Console.exe 23.07.2019 09:35:21, user: cxl

Here
Exit
Application was terminated in a non-standard way (e.g. exit(x) call or Ctrl+C)
Previous Topic: How i can play a sound?? wav,mp3,ogg,aiff????
Next Topic: Filtering an ArrayCtrl
Goto Forum:
  


Current Time: Thu Mar 28 23:10:22 CET 2024

Total time taken to generate the page: 0.01634 seconds