GET@h

Hi All,

I wrote an application in c++ listening to incoming UDP data from the network. When I close the application, the process is still alive and I can see it in the task manager even though I killed all my threads, disposed all the ressources and closed all opened sockets.

The process does not stay alive for a long time: The more my application runs, the longer the process stays alive after closing the application. As an example:

if my application was runnig for 5mn, the process is killed right away after my application is closed.

if my application was runnig for 2 hours, the process is killed after about 5mn after my application is closed.

if my application was runnig for 2 days, the process is killed after more then 10mn after my application is closed.

Has anyone of you encountred this kind of problem before

Thanks for help.




Re: Visual C++ General Process not terminated after closing application.

Ramkrishna Pawar

Is it a UI application How are you closing it Have you use PostQuitMessage anywhere Is there any thread and/or synchronisation (WaitForSingleObject etc.) calls Have you tried debugging to see what code keep on executing






Re: Visual C++ General Process not terminated after closing application.

GET@h

Hi,

Thanks for the quick answer.

Yes, it is a Dialog based application. I close it just by calling OnOk()/OnCancel. I overrided the Close member function this way:

Code Snippet

void MyDlg::OnClose(){

// TODO: Add your message handler code here and/or call default

DisposeRessources();

CDialog::OnClose();

}

And here is the DisposeRessources function:

Code Snippet

void MyDlg::DisposeRessources(){

// I used a mailslot for communication with other applications.

if(mailSlot != NULL)

delete mailSlot;

if(udpSocket != NULL){

udpSocket->Close();

delete udpSocket;

}

}

I used a WaitForSingleObject to kill the mailSlot since I have a thread listening for incoming data from the mail slot.

The thread is killed like this in the destructor of the mailslot.

Code Snippet

MailSlot::~MailSlot(void){

if(hMailSlotThread) {

KillMailSlot.SetEvent();

WaitForSingleObject(hMailSlotThread, 500);

CloseHandle(hMailSlotThread);

hMailSlotThread = NULL;

}

}

The WaitForSingleObject is not blocking I guess (500ms at most)

The debuger dumps data right after my application is closed. The dump displays a message that it's dumping data from the file where I defined my CAsyncSocket even though the socket is closed and deleted (I can see that the port I opened is closed using "netstat -a" command)

Thanks for help,






Re: Visual C++ General Process not terminated after closing application.

Ramkrishna Pawar

WaitForSingleObject call IS A blocking call, it's blocks executaion untill the condition (of timeout or availability of handle) is satisfied. Have you not debugged it after closing the dialog See if the mailslot wait keeps the process hanging.






Re: Visual C++ General Process not terminated after closing application.

crescens2k

Well, I would be surprised here if the Wait keeps the process hanging. If it is then that means the timeout for the Wait function isn't working.

If anything it could be the close handle which is blocking. But well, debugging is the only way to find where the process is hanging.






Re: Visual C++ General Process not terminated after closing application.

Ramkrishna Pawar

crescens2k wrote:

Well, I would be surprised here if the Wait keeps the process hanging. If it is then that means the timeout for the Wait function isn't working.

If anything it could be the close handle which is blocking. But well, debugging is the only way to find where the process is hanging.

Yes, I definitly overlooked that. Smile






Re: Visual C++ General Process not terminated after closing application.

GET@h

Hi,

Yes, I checked this. The WaitForSingleObject is not blocking for more then 500ms. So, the problem is coming from somewhere else I guess.

Thanks






Re: Visual C++ General Process not terminated after closing application.

Simple Samples

Are you sure that MyDlg::OnClose is executed It has been a while since I did dialogs but I am nearly certain that the WM_CLOSE message is not processed for a dialog; at least not for a MFC dialog.

If it were me, I would not use a dialog for an application such as that but I can't think of a reason why a dialog would be a problem. A regular window with a view is just more flexible. I know that the document/view architecture does more than we usually need but I usually develop my MFC programs without a of it. Well I guess it is necessary to be familiar with MFC to be able to create a window with a view that does not carry unnecessary baggage.

I assume the dialog is modeless but I think OnClose is not executed whether the dialog is modal or modelss. For a modeless dialog DestroyWindow must be called; if not, then the the dialog becomes invisible but does not die. Look at the CDialog :: OnOK documentation.

Is MyDlg created in the heap If so, then it should be explicitly deleted. Technically, it probably does not matter, but C++ programmers don't like to leak memory even if it is harmless.






Re: Visual C++ General Process not terminated after closing application.

GET@h

Hi,
Sorry for the late answer.
I'll check this (if the OnClose is called) tomorrow morning and I'll keep you posted.

Thanks,





Re: Visual C++ General Process not terminated after closing application.

Simple Samples

I am eager to know the explanation but anytime you get to it is fine by me.

I have since found samples that override CDialog :: OnClose so I can't say that it is not processed. All I know (that I can remenber at least) is that I have never gotten an override of CDialog :: OnClose to execute.






Re: Visual C++ General Process not terminated after closing application.

GET@h

Hi,

You are right, when the Ok/Cancel button is clicked, then the OnClose member function is not called. It's called only when the close button on the title bar is clicked.

The problem is then resolved.

Thanks guys,