News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Closing the program cleanly

Started by ragdogz, January 28, 2009, 11:50:25 AM

Previous topic - Next topic

ragdogz

I have a program uses winsock. if i press close button when socket is in use, do i have to close the socket first and cleanup like this following code?

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
...
...
.elseif uMsg==WM_CLOSE
invoke closesocket,socket
invoke WSACleanup
invoke EndDialog, hWnd,NULL
...
...
DlgProc endp


or just invoke EndDialog, hWnd,NULL will handle all?

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
...
...
.elseif uMsg==WM_CLOSE
invoke EndDialog, hWnd,NULL
...
...
DlgProc endp


i notice no difference effect with or without cosing socket and cleanup first. but in similiar program made with VB, if i don't close the socket first then i will get error after the program closed..

thx..

ToutEnMasm

Hello,
As always,answer is in the SDK documentation or winhelp.

Quote
An application should always have a matching call to closesocket for each successful call to socket to return any socket resources to the system


ragdogz

so it means, i have to close the socket which previously i successfuly loaded, before closing the program to return any socket resources to the system?
what if i didn't close the socket?
will the socket resources stay remain in the memory?

i'm confused with this, since there's nothing bad behaviour if i close my application without closing the socket first..

kromag

The resources would then, usually, get returned upon a system reboot.

Tedd

As always, you should close any handles you create (files, memory, sockets, gdi objects, etc) - if only for good behaviour.
In many cases you can usually get away with not closing anything, and it will be cleaned up during ExitProcess, but that doesn't mean you should, and it doesn't mean it will work correctly all the time. So just do it - unless you have a very good reason not to, and then still do it :P
No snowflake in an avalanche feels responsible.

ragdogz

okay, i'll close the socket..
better prepare before the chaos comes to my system  :green2
thank you all..

now i want to know what's the different between invoke EndDialog, hWnd,NULL and invoke ExitProcess,0
they both close my program..

ragdog

Hi

says the  name of api

For more informtion about apiĀ“s read Iczelion's tutorial Series
or msdn http://msdn.microsoft.com/en-us/library/ms645472(VS.85).aspx  :U


donkey

EndDialog does not close your program, it simply destroys the dialog and child windows and stops the message loop, ExitProcess does quite a bit more, it allows for a graceful shutdown of all threads owned by the process (even if you think you only have one thread there can still be multiple threads owned by your process i.e. COM), closes any open handles, detaches any DLLs loaded, releases memory and signals waitable objects and mutexes. One is definitely not a replacement for the other, each has their place in your program. You can get away without calling EndDialog (though I wouldn't recommend it) but you should always call ExitProcess to terminate your application.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

gwapo

Quote from: busclock on January 29, 2009, 06:56:29 AM
now i want to know what's the different between invoke EndDialog, hWnd,NULL and invoke ExitProcess,0
they both close my program..

EndDialog is not for closing programs, it's only for destroying dialogs. However, I've read somewhere in MSDN that it's always a good practice to use DestroyWindow because it does better job if you are working on many dialogs, and/or modeless dialogs.

The reason your program closes after your call to EndDialog is because you only have one main window in UI thread, and you happen to close that one main window.

ExitProcess is for terminating the entire process (destroying all windows, threads etc) and return control back to host operating system.

HTH,

-chris