C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[60444] DeleteCriticalSection() 호출시 메모리 오류가 납니다..
프로그램초보 [] 2381 읽음    2010-03-13 19:34
안녕하세요 초보입니다;

주요 소스는 밑에 있구요..

디버거가 익셉션 일으킬때

소멸자의 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);

	}
};

+ -

관련 글 리스트
60444 DeleteCriticalSection() 호출시 메모리 오류가 납니다.. 프로그램초보 2381 2010/03/13
60489     Re:DeleteCriticalSection() 호출시 메모리 오류가 납니다.. 송신영 2293 2010/03/18
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.