开发者

Why can't my MFC app exit completely?

I made a MFC application which probably has two threads, one for receiving data from a socket using UDP protocol and one is the main thread of MFC app. While any data is received some objects, created in the main thread by new operator, would be notified to fetch the data through apply the observer design pattern. The problem is that sometimes after I clicked the close system button, the GUI of the app disappeared, but its process can still be found in the Task Manager. If I stop the data source (UDP client) this problem would never happen. Other important and maybe helpful information is listed below:

  1. The Observer design pattern was implemented with STL container list. I have used the critical section protection in the Attach, Detach and Notify functions.
  2. I deleted the observer objects before closing the UDP socket.
  3. The data transfer rat开发者_如何学Ce may be a little faster than process data, because after closing the data source the data process is still working.

I can't figure out what lead my app can not exit completely. Please give me some clues.


This is usually caused by a thread you created and not exit it programmatically when you exit the appliation. There must be a while clause in your thread. The way to find where it is still running is:

  1. use debug mode to start you application and click the exit button the top right corner to exit it.

  2. Check from task manager and see if it is still running

  3. if it is, excute Debug->Break All,

  4. Open threads windows, double click each thread, you will find where your code is still looping.


Typically a process won't terminate because there's still a foreground thread running somewhere. You must ensure that your socket library isn't running any thread when you want to close your application.


First thing, with MFC, please use the notification based methods to get notifications on message arrivals, connections etc. So you can get rid of threads if you have.

It's quite easy to attache to a debugger and Break see which threads are existing and waiting for what.

Alternatively you can use ProcessExplorer with proper symbol configuration to see the call stacks of the threads available for the particular process.

The application can two kind of issues to exit, one could be infinite loop and other might be waiting/deadlock (e.g. socket read command is a blocking call). You can easily deduce the problem by attaching to debugger.

Otherwise please provide further information about the threads, code snippet possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜