我用VS.Net 2002, 2003怎么这么不稳定!郁闷死了!
sxbyl 2003-05-15 08:40:37 前一阵子是用VS.Net 2002写VC程序,发现VS.Net 2002很不稳定,界面上浮动窗口经常出现的一些问题就不说了,有一个让我非常郁闷的问题就是有时当我双击代码中的单词时,整个界面会没有反应,最近重装了系统,装了VS.2003,浮动窗口那些问题倒是没了,不过在编辑代码时,在编辑区双击偶尔还是会界面死掉,只好强行关掉。
这绝对不是我配置的问题,我的机子AMD 2000+, 768 M内存,肯定不会是因为性能导致没反应(而且一直都会不到正常状态)。
我的操作系统是XP SP1,你们谁还碰到过类似的问题?而且我还发现我在编辑特定一段代码的时候很容易出现这个问题,而大多数代码怎么点都没事,怪事。
我现在把经常导致VS.Net界面没反应的那段代码贴出来,大家在自己的机子上试试,看会不会出现同样的问题。
//+---------------------------------------------------------------------------
//
// File: AutoBuffer.h
//
// Contents:
//
// Classes:
//
// Functions:
//
// History: 5-11-2003 sxbyl Created
//
//----------------------------------------------------------------------------
#pragma once
#include <afxmt.h>
namespace DolphinStudio
{
//+---------------------------------------------------------------------------
//
// Class : CPacket
//
// Synopsis : This class is a simple encapsulation of the pointer, not support
// the thread-safe property.
//
// History: 5-11-2003 sxbyl Created
//
//----------------------------------------------------------------------------
class CPacket
{
public:
CPacket();
~CPacket();
public:
//Attach to an existing pointer (takes ownership)
void Attach(LPVOID pData, size_t nBytes);
//Detach the pointer (releases ownership)
LPVOID Deatch();
//Delete the object pointed to
void Free();
//Gets the buffer's size
size_t GetSize() const;
//Returns the buffer's address
operator LPVOID() const;
//Allocates the buffer
bool Allocate(size_t nBytes);
//Copies the string pszData to the buffer, including the char '\0'.
bool WriteString(LPCTSTR pszData);
//Copies from pData to the packet's buffer.
void WriteData(LPVOID pData, size_t nBytes);
//Copies data to the buffer. The type T must can be bit-for-bit copied..
template<class T> void WriteData(const T &data);
//Sets the current pos of the packet's buffer.
bool SetCurrentPos(size_t nPos);
size_t GetCurrentPos() throw();
private:
LPVOID m_pBuffer;
size_t m_nSize;
size_t m_nCurrentPos;
};
inline CPacket::CPacket() :
m_pBuffer(NULL),
m_nCurrentPos(0),
m_nSize(0)
{
}
inline CPacket::~CPacket()
{
Free();
}
inline bool CPacket::SetCurrentPos(size_t nPos)
{
ASSERT(nPos < m_nSize);
if(nPos >= m_nSize)
return false;
m_nCurrentPos = nPos;
return true;
}
inline void CPacket::Free()
{
if(m_pBuffer != NULL)
{
delete[] m_pBuffer;
m_pBuffer = NULL;
m_nSize = 0;
}
}
inline bool CPacket::Allocate(size_t nBytes)
{
ASSERT(m_pBuffer == NULL);
m_pBuffer = new BYTE[nBytes];
if(m_pBuffer == NULL)
{
return false;
}
m_nSize = nBytes;
return true;
}
inline void CPacket::Attach(LPVOID pData, size_t nBytes)
{
ASSERT(m_pBuffer = NULL);
m_pBuffer = pData;
m_nSize = nBytes;
}
inline LPVOID CPacket::Deatch()
{
LPVOID p = m_pBuffer;
m_pBuffer = NULL;
return p;
}
inline size_t CPacket::GetSize() const
{
return m_nSize;
}
inline CPacket::operator LPVOID() const
{
return m_pBuffer;
}
template<class T> inline void CPacket::WriteData(const T &data)
{
ASSERT(m_nCurrentPos + sizeof(T) <= m_nSize);
*((T *)((LPBYTE)m_pBuffer + m_nCurrentPos))=data;
m_nCurrentPos += sizeof(T);
}
inline bool CPacket::WriteString(LPCTSTR pszData)
{
size_t nSize=_tcslen(pszData)+sizeof(TCHAR);
if(m_nCurrentPos + nSize > m_nSize)
return false;
memcpy(m_pBuffer, pszData, nSize);
return true;
}
inline void CPacket::WriteData(LPVOID pData,size_t nBytes)
{
ASSERT(m_nCurrentPos + nBytes <= m_nSize);
memcpy((LPBYTE)m_pBuffer + m_nCurrentPos, pData, nBytes);
}
inline size_t CPacket::GetCurrentPos()
{
return m_nCurrentPos;
}
//+---------------------------------------------------------------------------
//
// Class : CAutoBuffer
//
// Synopsis : This a thread-safe class to manage the memory. The buffer can be
// dynamically increased
//
// History: 5-11-2003 sxbyl Created
//
//----------------------------------------------------------------------------
class CAutoBuffer
{
public:
CAutoBuffer();
~CAutoBuffer();
//Allocates the buffer
bool Allocate(size_t nBytes);
//Reallocate the buffer. If bCopyData == true, the old buffer's data will be copied to the new buffer
bool Reallocate(size_t nBytes, bool bCopyData);
//Delete the object pointed to
void Release();
private:
bool ReallocateWithoutLock(size_t nBytes, bool bCopyData);
private:
LPVOID m_pBuffer; //the head of the whole buffer
LPVOID m_pBufferTail; //the tail of the while buffer
LPVOID m_pBufferUsedHead; //the head pointer of the used buffer
LPVOID m_pBufferUsedTail; //the tail pointer of the used buffer
size_t m_nUsedBufferSize; //the used buffer size
size_t m_nBufferSize; //the size of the whole buffer
size_t m_nDefaultSize; //default size of the buffer
HANDLE m_hEventHasData; //If has some data to read, the handle will be signaled
bool m_bUnusedBufferLocked; //Is the unused buffer locked ?
size_t m_nDefaultAutoBufferSize; //the default size of the object's buffer
CCriticalSection m_csLock; //Make the object thread safe
private:
static const size_t DEFAULT_AUTO_BUFFER_SIZE;
static const size_t DEFAULT_AUTO_BUFFER_MAX_SIZE;
};
const size_t CAutoBuffer::DEFAULT_AUTO_BUFFER_SIZE = 1024 * 4;
const size_t CAutoBuffer::DEFAULT_AUTO_BUFFER_MAX_SIZE = 1024 * 1024 * 4;
inline CAutoBuffer::CAutoBuffer()
{
m_pBuffer = NULL;
m_pBufferUsesdHead = NULL;
m_pBufferUsedTail = NULL;
m_pBufferTail = NULL;
m_bUnusedBufferLocked = false;
m_nUsedBufferSize = 0;
m_nBufferSize = 0;
m_nDefaultSize = m_nDefaultAutoBufferSize;
m_hEventHasData=CreateEvent(NULL,FALSE,FALSE,NULL);
}
inline CAutoBuffer::~CAutoBuffer()
{
Release();
CloseHandle(m_hEventHasData);
m_hEventHasData = NULL;
}
inline void CAutoBuffer::Release()
{
CSingleLock l(&m_csLock,TRUE);
if(m_pBuffer != NULL)
{
delete[] m_pBuffer;
m_pBuffer = NULL;
m_pBufferUsedHead = NULL;
m_pBufferUsedTail = NULL;
m_pBufferTail = NULL;
m_nUsedBufferSize = 0;
m_nBufferSize = 0;
}
ASSERT(!(m_pBufferUsedHead || m_pBufferUsedTail || m_pBufferTail || m_nUsedBufferSize || m_nBufferSize));
}
inline bool CAutoBuffer::Allocate(size_t nBytes)
{
CSingleLock l(&m_csLock,TRUE);
ASSERT(m_pBuffer == NULL && m_bUnusedLocked == false);
m_pBuffer = new BYTE[nBytes];
if(m_pBuffer == NULL)
return false;
m_pBufferUsedHead = m_pBuffer;
m_pBufferUsedTail = m_pBuffer;
m_pBufferTail = (LPBYTE)m_pBuffer + nBytes;
return true;
}
}