socket编程里的一段代码:void CTcpServerDlg::OnDestroy() 
{
CDialog::OnDestroy();
//关闭SOCKET
closesocket(m_server);
for(UINT i=0; i<m_nClients; i++)
{
if(m_clients[i] > 0)
closesocket(m_clients[i]);
}
//清理环境
WSACleanup();
}
问题是:已经执行CDialog::OnDestroy();了,后面那些语句还有用吗? 

解决方案 »

  1.   

    后面那些语句应该写在CDialog::OnDestroy();之前
      

  2.   

    我感觉也应该放在之前,麻烦简单介绍一下CDialog::OnDestroy()工作机制吧,它销毁的是CTcpServerDlg这个类吧。
      

  3.   


    好像是那么回事,跟堆栈什么有关吧,想了解其中的一些调用机制。还有CDialog::OnDestroy();究竟做了哪些工作。高手来补充补充
      

  4.   

    MSDN里的解释:
    CWnd::OnDestroy
    This method is called by the framework to inform the CWnd object that it is being destroyed. OnDestroy is called after the CWnd object is removed from the screen. OnDestroy is called first for the CWnd being destroyed, then for the child windows of CWnd as they are destroyed. It can be assumed that all child windows still exist while OnDestroy runs.个人以为,你的代码是释放socket方面的内容,于窗口的操作无关,放在destroy前后应该不会有差别。
      

  5.   

    与窗口操作是无关,但是socket初始化时,就是在窗口类里进行的。如果destroy了,应该意味着窗口类也析构了吧,那么怎么跟socket方面无关呢?(本人很菜,说错了别笑话,请指点)初始化代码如下:
    BOOL CTcpServerDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    //变量初始化
    m_nClients = 0;
    memset(m_clients, 0, sizeof(m_clients));
    //初始化环境
    WSADATA wd = {0};
    int nStart = WSAStartup(MAKEWORD(SOCK_VER, 0), &wd);
    if(nStart = 0)
    {
    return TRUE;
    }
    if(LOBYTE(wd.wVersion) != 2)
    {
    return TRUE;
    }
    //创建SOCKET
    m_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(m_server == INVALID_SOCKET)
    {
    ErrMsg(WSAGetLastError());
    return TRUE;
    }
    return TRUE;  // return TRUE  unless you set the focus to a control
    }
      

  6.   

    销毁的是CTcpServerDlg对应的窗口,而不是CTcpServerDlg类
    这个时候对窗口的操作比如setwindowtext不能调用了,但是类的方法可以调用(只要没有依赖HWND的方法),因为类还没销毁CTcpServerDlg类析构函数调用以后才销毁lz还没搞清楚windows的窗口跟窗口类之间的关系就你的这段代码来说放在析构函数都没问题
      

  7.   

    楼上说的正中要害,看来基础没打好啊。直接学socket编程还很费劲