|
|
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: 93 Registered: September 2020 Location: Brasil Valinhos SP
|
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: 862 Registered: August 2007
|
Experienced 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
upp-components: https://github.com/ismail-yilmaz/upp-components
[Updated on: Thu, 04 February 2021 16:34] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Mar 05 13:32:59 CET 2021
Total time taken to generate the page: 0.02962 seconds
|
|
|