-4

I know that it is possible to close an application by calling the exit(0) function. And that by using it, all heap-allocated memory prior to the call is cleared. So you do not have to worry about it.

But to debug your program and investigate better if there is a memory leak, it is not practical to close the program without first releasing all memory.

So I wanted to schedule the closure by adding the same message that the system sends by pressing the X button on a window (WM_CLOSE).

With that message(or event), the program can execute the code that will close the application.

So the question is if there is any function, or something from the winapi that adds that message to the current process?

Mano-Wii
  • 592
  • 5
  • 19
  • 2
    The message loop receives the `WM_CLOSE` message. – Paul Ogilvie Mar 24 '18 at 14:30
  • Pretty much wrong, all around. The analysis is, how some developer might think things are. This is not the case. For one, not every window provides the ability to close it through a button. Those that do, can do it in a number of different ways. The standard Windows way is to have a system menu entry, that eventually causes the close button to send a `WM_CLOSE` message to the window's window procedure. The default implementation (`DefWindowProc`) responds by calling `DestroyWindow` on the window handle. – IInspectable Mar 24 '18 at 14:35
  • Regardless of the edits you keep pushing, you have a history of asking about [XY Problems](http://xyproblem.info). Pretty sure, this is the case here, too. So what are you ultimately trying to accomplish? – IInspectable Mar 24 '18 at 14:38
  • @IInspectable, I simply want to schedule the closing of an application. I can not call `exit(0)` inside a function that are not able to free some of the heap datas created before. I'll try the `DestroyWindow ` you mentioned ;) – Mano-Wii Mar 24 '18 at 14:43
  • Not freeing heap-allocated memory prior to calling `exit(0)` doesn't change one thing. You are tearing down a process. Why clean up prior to tearing down the house? And how do you propose to get into the target process, and *"schedule the closing of an application"* (whatever that is supposed to mean). – IInspectable Mar 24 '18 at 15:07
  • @IInspectable unfortunately, generations of bad textbooks and worse profs have indoctrinated developers into believing that neglecting to explicitly free heap memory before terminating a process is the fifth horse of the apocalypse and an extinction level event. It's a faith-based thing that cannot be argued with:( – Martin James Mar 24 '18 at 15:18
  • I'm unable to ask more questions. stackoverflow does not accept any more questions from me :( – Mano-Wii Apr 24 '18 at 00:37

2 Answers2

1

You can use WINAPI SendMessage with WM_CLOSE for the Msg parameter.

Here more about this function: SendMessage

Mano-Wii
  • 592
  • 5
  • 19
-1

if you Can Replace WM_CLOSE With SIGINT You Can Do a Simple Signal On SIGINT That Calls Your own Function (Don't Know That You Can Use It On WM_CLOSE But Tested with SIGINT On Windows)

here's Example Of Signal In MSDN

here's how to emit SIGINT Here

Mohammad Ali
  • 569
  • 4
  • 10