结构体置0的问题
程序中常常需要对结构体置0,但是要注意:
如果结构体中有类,不能置0;置0会导致已经实例化的类变量,地址变为0
如果结构体中有其它结构体,也要当心,如果所包含的结构体中有指针,而指针已经分配了地址(如new过),此时置0,会导致内存泄漏
如:
struct ClientContext { SOCKET m_Socket; ///Socket char m_IP[20]; ///IP WORD nPort; ///端口 // Store buffers CLeesBuffer* m_pReadBuffer; ///读BUFFER CLeesBuffer* m_pWriteBuffer; ///写BUFFER // Input Elements for Winsock WSABUF m_wsaInBuffer; ///socket 读缓冲 BYTE m_byInBuffer[SOCKET_BUFFERSIZE]; ///读缓冲 // Output elements for Winsock WSABUF m_wsaOutBuffer; HANDLE m_hWriteComplete; HANDLE m_hDisconnect; // Message counts... purely for example purposes LONG m_nMsgIn; LONG m_nMsgOut; OVERLAPPEDPLUS* pOverlap_ioRead; OVERLAPPEDPLUS* pOverlap_ioWrite; OVERLAPPEDPLUS* pOverlap_ioInit; OVERLAPPEDPLUS* pOverlap_ioIdle; CTime m_tLastActiveTime; //以下为用户逻辑部分 BOOL m_bIsHandShaked; ///是否已经完成握手 };