CRITICAL_SECTION csc; 변수를 맴버 변수로 넣지 말고 글로벌로 빼보세요.
그리고 크리티컬 사용시에는 항상 아래와 같이 try __finally 로 처리를 해줘야합니다.
만약 Entetr.. 후에 Leave.. 하기 전에 error가 발생하면 Leave가 호출되지 못하게 되고
프로그램은 데드락에 걸립니다.
try {
EnterCriticalSection(&csc);
.......
}
__finally{
LeaveCriticalSection(&csc);
}
프로그램초보 님이 쓰신 글 :
: 안녕하세요 초보입니다;
:
: 주요 소스는 밑에 있구요..
:
: 디버거가 익셉션 일으킬때
:
: 소멸자의 DeleteCriticalSection() 요 함수부분이 표시 되는데요...
:
: 어디가 잘못된건지 알려주셨으면 합니다.. ㅠㅠ;;
:
: 오류 내용은..
:
: EAccessViolation.. access violation read of 72657375
:
: vcl120.bpl
:
: 희안한건 컴퓨터를 껐다 켜도 메모리 번지가 항상 같네요..
:
:
:
:
:
:
: class CClientContext //To store and manage client related information
: {
: private:
:
: OVERLAPPED *m_pol;
: WSABUF *m_pwbuf;
:
: int m_nTotalBytes;
: int m_nSentBytes;
:
: bool m_bHasProcess;
:
: char m_userId[USER_ID_LEN];
: SOCKET m_Socket; //accepted socket
: int m_nOpCode; //will be used by the worker thread to decide what operation to perform
: char m_szBuffer[MAX_BUFFER_LEN];
: std::vector m_CProcess;
: CRITICAL_SECTION csc;
: public:
:
: //Get/Set calls
: void SetOpCode(int n)
: {
: m_nOpCode = n;
: }
:
: int GetOpCode()
: {
: return m_nOpCode;
: }
:
: void SetTotalBytes(int n)
: {
: m_nTotalBytes = n;
: }
:
: int GetTotalBytes()
: {
: return m_nTotalBytes;
: }
:
: void SetSentBytes(int n)
: {
: m_nSentBytes = n;
: }
:
: void IncrSentBytes(int n)
: {
: m_nSentBytes += n;
: }
:
: int GetSentBytes()
: {
: return m_nSentBytes;
: }
:
: void SetSocket(SOCKET s)
: {
: m_Socket = s;
: }
:
: SOCKET GetSocket()
: {
: return m_Socket;
: }
:
: void GetUserId(char *userId)
: {
: strcpy(userId, m_userId);
: }
:
: void SetUserId(char *userId)
: {
: strcpy(m_userId, userId);
: }
:
: void removeUserId()
: {
: ZeroMemory(m_userId, sizeof(m_userId));
: }
:
: void ZeroBuffer()
: {
: ZeroMemory(m_szBuffer, MAX_BUFFER_LEN);
: }
:
: void SetWSABUFLength(int nLength)
: {
: m_pwbuf->len = nLength;
: }
:
: int GetWSABUFLength()
: {
: return m_pwbuf->len;
: }
:
: WSABUF* GetWSABUFPtr()
: {
: return m_pwbuf;
: }
:
: OVERLAPPED* GetOVERLAPPEDPtr()
: {
: return m_pol;
: }
:
: void ResetWSABUF()
: {
: ZeroBuffer();
: m_pwbuf->buf = m_szBuffer;
: m_pwbuf->len = MAX_BUFFER_LEN;
: }
:
: CProcess * GetProcess(){
: std::vector ::iterator IterProcess;
: IterProcess = m_CProcess.begin();
: return *IterProcess;
: }
:
: void AddToProcessList(CProcess *pCProcess)
: {
: m_bHasProcess = true;
:
: EnterCriticalSection(&csc);
: m_CProcess.push_back(pCProcess);
: LeaveCriticalSection(&csc);
: }
:
: void RemoveFromProcessListAndFreeMemory()
: {
: if (m_bHasProcess == true)
: {
: m_bHasProcess = false;
:
: EnterCriticalSection(&csc);
: m_CProcess.erase(m_CProcess.begin());
: LeaveCriticalSection(&csc);
: }
: }
:
: bool CProcessEmpty() {
: return m_CProcess.empty();
: }
:
: bool HasCProcess() {
: return m_bHasProcess;
: }
:
: void CleanProcessList()
: {
: EnterCriticalSection(&csc);
:
: std::vector ::iterator IterProcess;
: for (IterProcess = m_CProcess.begin(); IterProcess != m_CProcess.end( ); IterProcess++) {
: //i/o will be cancelled and socket will be closed by destructor.
: delete *IterProcess;
: }
:
: m_CProcess.clear();
: LeaveCriticalSection(&csc);
: }
:
: //Constructor
: CClientContext()
: {
: m_pol = new OVERLAPPED;
: m_pwbuf = new WSABUF;
:
: ZeroMemory(m_pol, sizeof(OVERLAPPED));
:
: m_Socket = SOCKET_ERROR;
:
: ZeroMemory(m_szBuffer, MAX_BUFFER_LEN);
: ZeroMemory(m_userId, USER_ID_LEN);
:
: m_pwbuf->buf = m_szBuffer;
: m_pwbuf->len = MAX_BUFFER_LEN;
:
: m_nOpCode = 0;
: m_nTotalBytes = 0;
: m_nSentBytes = 0;
:
: m_bHasProcess = false;
:
: InitializeCriticalSection(&csc);
:
: printLog("\n\n[CClientContext] 클라이언트 객체 생성자 호출");
: }
:
: //destructor
: ~CClientContext()
: {
: //Wait for the pending operations to complete
: while (!HasOverlappedIoCompleted(m_pol))
: {
: Sleep(0);
: }
:
: closesocket(m_Socket);
:
: //Cleanup
: delete m_pol;
: delete m_pwbuf;
:
: if (m_bHasProcess) {
: CleanProcessList();
: printLog("[~CClientContext] Process 리스트 삭제 완료");
: }
: else {
: printLog("[~CClientContext] Process 리스트 없음");
: }
:
: DeleteCriticalSection(&csc);
:
: }
: };
: |