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();了,后面那些语句还有用吗?
{
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();了,后面那些语句还有用吗?
好像是那么回事,跟堆栈什么有关吧,想了解其中的一些调用机制。还有CDialog::OnDestroy();究竟做了哪些工作。高手来补充补充
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前后应该不会有差别。
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
}
这个时候对窗口的操作比如setwindowtext不能调用了,但是类的方法可以调用(只要没有依赖HWND的方法),因为类还没销毁CTcpServerDlg类析构函数调用以后才销毁lz还没搞清楚windows的窗口跟窗口类之间的关系就你的这段代码来说放在析构函数都没问题