VirtualAlloc的奇怪现象,不知原因
结构体:
struct ClientContext { SOCKET m_Socket; ///Socket char m_IP[20]; ///IP WORD nPort; ///端口 // Store buffers CLeesBuffer m_ReadBuffer; ///读BUFFER CLeesBuffer m_WriteBuffer; ///写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; ///是否已经完成握手 };
其中CLeesBuffer是在前人(CBuffer)的基础上修改的内存管理类。
如果像上面这样用,会出问题:
两个实例在CLeesBuffer中VirtualAlloc申请的内存地址,会相同(Win10 64X VS2017)。多次跟踪,结果相同。VirtualQuey查询结果:
。。。。。。。。。。。。。 07:05:50.725 CLeesBuffer.ReAllocateBuffer 【Before MEM_COMMIT】 (ThreadID:0x00003afc this=0x03145800) nNowSize=28 pNewBuf=0x00d00000 memInfo.BaseAddress= 0x00d00000 memInfo.AllocationBase= 0x00d00000 memInfo.AllocationProtect=0x00000001 memInfo.RegionSize= 0x00001000 memInfo.State= 0x00002000 memInfo.Protect= 0x00000000 memInfo.Type= 0x00020000 07:05:50.725 CLeesBuffer.ReAllocateBuffer MEM_COMMIT (ThreadID:0x00003afc this=0x03145800) nNowSize=28 pNewBuf=0x00d00000 memInfo.BaseAddress= 0x00d00000 memInfo.AllocationBase= 0x00d00000 memInfo.AllocationProtect=0x00000001 memInfo.RegionSize= 0x00001000 memInfo.State= 0x00001000 memInfo.Protect= 0x00000004 memInfo.Type= 0x00020000 07:05:50.725 (this=0x03145800)VirtualAlloc(MEM_COMMIT):m_pBase=0x00000000 pNewBuf=0x00d00000
【在此故意延迟100ms】也是同样结果
07:05:50.828 (this=0x03146040)VirtualAlloc:m_pBase=0x00000000 pNewBuf=0x00d10000 07:05:50.828 CLeesBuffer.ReAllocateBuffer 【Before MEM_COMMIT】 (ThreadID:0x00003afc this=0x03146040) nNowSize=28 pNewBuf=0x00d00000 memInfo.BaseAddress= 0x00d00000 memInfo.AllocationBase= 0x00d00000 memInfo.AllocationProtect=0x00000001 memInfo.RegionSize= 0x00001000 memInfo.State= 0x00002000 memInfo.Protect= 0x00000000 memInfo.Type= 0x00020000 07:05:50.829 CLeesBuffer.ReAllocateBuffer MEM_COMMIT (ThreadID:0x00003afc this=0x03146040) nNowSize=28 pNewBuf=0x00d00000 memInfo.BaseAddress= 0x00d00000 memInfo.AllocationBase= 0x00d00000 memInfo.AllocationProtect=0x00000001 memInfo.RegionSize= 0x00001000 memInfo.State= 0x00001000 memInfo.Protect= 0x00000004 memInfo.Type= 0x00020000 。。。。。。。。。。。
后来改为指针,用new来实例,就没问题了
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; ///是否已经完成握手 };