|
|
Home » U++ Library support » TopWindow&PopUp, TrayIcon » TopWindow when Close override show exception unduly
TopWindow when Close override show exception unduly [message #56181] |
Thu, 04 February 2021 15:51  |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi,
when I create a simple application as shown below and rewrite the close method, when executing the application
memory exception is reported
(under windows 10 / 64bits /memory 8GB / TheIDE 15040 / memory free 42%)
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class SMain : public TopWindow {
public:
typedef SMain CLASSNAME;
SMain();
void Close() override {
delete this; // error show heap is corrupted !!!!
}
};
SMain::SMain()
{
}
GUI_APP_MAIN
{
SMain se;
se.Run();
}
in the log file
Heap is corrupted --memory-breakpoint__ 3197158753
Memory at 0x000000000171FBF0, size 0x1 = 1
+0 0x000000000171FBF0 08 .
****************** PANIC: Heap is corrupted --memory-breakpoint__ 3197158753
If I don't rewrite the method to close, there is no exception. This means a bug!
Important: I also noticed that if you add a layout containing a menu also with an option to close
the screen pointed to the same method, the exception will no longer occur. Perhaps this is why this
issue has not yet become evident.
thanks
|
|
|
Re: TopWindow when Close override show exception unduly [message #56182 is a reply to message #56181] |
Thu, 04 February 2021 16:21   |
Oblivion
Messages: 1211 Registered: August 2007
|
Senior Contributor |
|
|
Hello BetoValle,
void Close() override {
delete this; // error show heap is corrupted !!!!
}
I don't think there is any bug in U++. You are trying to delete an object within the same object. That call to TopWindow::Close() will return to its caller, which is the TopWindow itself, and it will be a freed memory address. So you get heap corruption. Never do that. 
Not to mention the SMain instance (se) is allocated on stack. You don't need to delete it anyway.
TopWindow::Close() method is useful for cleaning up your code, if required. It is not where you delete a window. In fact, unless it is absolutely necessary, we avoid using 'delete' in U++.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Thu, 04 February 2021 16:34] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Jun 13 08:41:22 CEST 2025
Total time taken to generate the page: 0.04329 seconds
|
|
|