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 » 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 Go to next message
BetoValle is currently offline  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 Go to previous messageGo to next message
Oblivion is currently offline  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. Smile

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


[Updated on: Thu, 04 February 2021 16:34]

Report message to a moderator

Re: TopWindow when Close override show exception unduly [message #56189 is a reply to message #56181] Fri, 05 February 2021 03:48 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 93
Registered: September 2020
Location: Brasil Valinhos SP
Member
Hi, Thanks

i create this solution , after understant about ...


     void Close() override {
        delete this;     // now it works because it was instantiated as a pointer!!!
     }

GUI_APP_MAIN
{
	SMain* se;
	se = (new SMain);
	se->OpenMain();
	se->se.Run(); //Run when associed a especific window or "Ctrl::EventLoop()" when not associed a window 
                      //   and while any window open
 	
} 

 

Re: TopWindow when Close override show exception unduly [message #56190 is a reply to message #56189] Fri, 05 February 2021 13:22 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 93
Registered: September 2020
Location: Brasil Valinhos SP
Member
Hi,
(now I understood with you ) not to "normally" rewrite the method. There is a better "as an" event to do something before closing the window:
"WhenClosed" works even better, since I can incorporate some verification routine into it, and if I want to close the window, then I must call Close ().

 WhenClose = [=] { doSomething; };
 ...
 void SMain::doSomething()
 {
   ...
   ... 
   
   if(okGetout){ //and if necessary close
     Close();
   }
 }
Re: TopWindow when Close override show exception unduly [message #56193 is a reply to message #56182] Sun, 07 February 2021 10:31 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13145
Registered: November 2005
Ultimate Member
Oblivion wrote on Thu, 04 February 2021 16:21
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. Smile


That is actually OK if it is the last thing you do to the object, regardless it being in the object's method. Both by C++ standard and U++ Close. Well, should probably be documented.. Similar approach is used here: https://www.ultimatepp.org/examples$UWord$en-us.html

In fact, "delete this" is sort of the only "delete" that is allowed in U++ Smile

Quote:

Not to mention the SMain instance (se) is allocated on stack. You don't need to delete it anyway.


That is the real problem.

Mirek
Re: TopWindow when Close override show exception unduly [message #56194 is a reply to message #56193] Sun, 07 February 2021 11:20 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 862
Registered: August 2007
Experienced Contributor
Quote:
That is actually OK if it is the last thing you do to the object, regardless it being in the object's method. Both by C++ standard and U++ Close. Well, should probably be documented..

Got that, and indeed it should be documented.

Best regards,
Oblivion


Previous Topic: How to remove the button [X] that closes the window?
Goto Forum:
  


Current Time: Fri Mar 05 13:32:59 CET 2021

Total time taken to generate the page: 0.02962 seconds