使用std::map的时候一个奇怪的问题
我有一个一个SafeMap类定义和实现如下
SafeMap.h
#if !defined( _SAFEMAP_H_ )
#define _SAFEMAP_H_
class GList ;
#include "SafeList.h"
/////////////////////////////////////////////////////////////////////////////
// SafeMap command target
typedef map <int, void*> MAP_VOIDPTR;
typedef map <string, void*> MAP_STRING;
class SafeMap
{
private:
CRITICAL_SECTIONm_Lock;
// Attributes
public:
// Operations
public:
SafeMap();
virtual ~SafeMap();
// Overrides
public:
int GetCountString();
int GetCountInt();
int CopyTmpList( void* pList[] );
void CopyMap(SafeMap *pMap );
bool Delete( string pKey);
void * Find(string pKey);
void DeleteValue( void *pData );
void CopyList( SafeList *pList );
void StrCopyList(SafeList *pList );
void StrCopyTmpList(GList *pList );
void RemoveNULL() ;
void RemoveStrNULL() ;
void CopyTmpList( GList *pList );
MAP_VOIDPTRm_SafeMap;
MAP_STRINGm_SafeMapString;
LIST_VOIDPTRm_CopyList;
void Delete(MAP_STRING::iterator v) ;
void Delete(MAP_VOIDPTR::iterator v);
void Clear();
void * Find( int pKey );
bool Delete(int pKey);
bool Insert( intpKey, void* pData );
bool Insert( string pKey, void *pData );
void UnLock()
{
LeaveCriticalSection( &m_Lock );
};
void Lock()
{
EnterCriticalSection( &m_Lock );
};
MAP_VOIDPTR::iterator Begin()
{
return m_SafeMap.begin();
};
MAP_VOIDPTR::iterator End()
{
return m_SafeMap.end();
};
MAP_STRING::iterator BeginString()
{
return m_SafeMapString.begin();
};
MAP_STRING::iterator EndString()
{
return m_SafeMapString.end();
};
// Implementation
protected:
};
/////////////////////////////////////////////////////////////////////////////
#endif
////////////////////////////////////////////////////////////////////////////////////////
//SafeMap.cpp : implementation file
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SafeMap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////////////////////////////
// SafeMap
////////////////////////////////////////////////////////////////////////////////////////
SafeMap::SafeMap()
{
m_SafeMap.clear();
m_SafeMapString.clear();
m_CopyList.clear();
::InitializeCriticalSectionAndSpinCount( &m_Lock, 2000 );
}
SafeMap::~SafeMap()
{
::DeleteCriticalSection( &m_Lock );
}
////////////////////////////////////////////////////////////////////////////////////////
//Key & Data Insert
//
//-------------------------------------
//Return ValueExplain
//-------------------------------------
//true沥惑 贸府等 版快
//falseKey啊 吝汗等 版快
//-------------------------------------
////////////////////////////////////////////////////////////////////////////////////////
bool SafeMap::Insert(int pKey, void *pData)
{
// key狼 吝汗 咯何 Checking
//assert( m_SafeMap.find( pKey) == m_SafeMap.end() );
if( m_SafeMap.find(pKey) != m_SafeMap.end() )
return false;
Lock();
//data = m_SafeMap[key] <-- 单捞鸥 掘绢 坷扁
//MAP_VOIDPTR::value_type vt(pKey, pData);
//m_SafeMap.insert(vt);
m_SafeMap.insert( MAP_VOIDPTR::value_type(pKey, pData) );
UnLock();
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
bool SafeMap::Insert(string pKey, void *pData)
{
// key狼 吝汗 咯何 Checking
//assert( m_SafeMap.find( pKey) == m_SafeMap.end() );
if ((MAP_STRING::iterator)m_SafeMapString.find( pKey ) != m_SafeMapString.end() )
return false ;
Lock();
//data = m_SafeMap[key] <-- 单捞鸥 掘绢 坷扁
//MAP_VOIDPTR::value_type vt(pKey, pData);
//m_SafeMap.insert(vt);
m_SafeMapString.insert( MAP_STRING::value_type(pKey, pData) );
UnLock();
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
//秦寸 Key狼 单捞鸥甫 昏力窍绰 版快
////////////////////////////////////////////////////////////////////////////////////////
bool SafeMap::Delete(int pKey)
{
boolbReturn;
Lock();
// 漂沥 虐蔼狼 iterator 掘扁
MAP_VOIDPTR::iterator v = m_SafeMap.find( pKey );
if (v != m_SafeMap.end() )
{
// iterator甫 烹秦 夸家 昏力窍扁
m_SafeMap.erase( v );
bReturn=true;
}
else
{
bReturn=false;
}
UnLock();
return bReturn;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
bool SafeMap::Delete(string pKey)
{
boolbReturn;
Lock();
// 漂沥 虐蔼狼 iterator 掘扁
MAP_STRING::iterator v = m_SafeMapString.find( pKey );
if (v != m_SafeMapString.end() )
{
// iterator甫 烹秦 夸家 昏力窍扁
m_SafeMapString.erase( v );
bReturn=true;
}
else
{
bReturn=false;
}
UnLock();
return bReturn;
}
////////////////////////////////////////////////////////////////////////////////////////
//秦寸 iterlater肺 昏力窍绰 版快
////////////////////////////////////////////////////////////////////////////////////////
void SafeMap::Delete(MAP_STRING::iterator v)
{
Lock();
m_SafeMapString.erase( v++ );
UnLock();
return ;
}
////////////////////////////////////////////////////////////////////////////////////////
//秦寸 iterlater肺 昏力窍绰 版快
////////////////////////////////////////////////////////////////////////////////////////
void SafeMap::Delete(MAP_VOIDPTR::iterator v)
{
Lock();
m_SafeMap.erase( v++ );
UnLock();
return ;
}
////////////////////////////////////////////////////////////////////////////////////////
//漂沥 Key甫 茫酒 馆券 矫虐绰 版快
////////////////////////////////////////////////////////////////////////////////////////
void* SafeMap::Find(int pKey)
{
//2003.08.30 Duke Kim Server Donw栏肺 荐沥
//Lock 风凭 眠啊 眠饶 加档 巩力 绊妨
Lock();
MAP_VOIDPTR::iterator v= m_SafeMap.find( pKey );//漂沥 虐蔼狼 iterator 掘扁
MAP_VOIDPTR::iterator v_end= m_SafeMap.end();//End iterator 掘扁
UnLock();
// Data啊 绝绰 版快 贸府
if ( v == v_end )
{
return NULL;
}
//iterator甫 烹秦 单捞磐 掘扁
// void *inst = ;
return v->second;
}
void * SafeMap::Find( string pKey )
{
//2003.08.30 Duke Kim Server Donw栏肺 荐沥
//Lock 风凭 眠啊 眠饶 加档 巩力 绊妨
Lock();
MAP_STRING::iterator v= m_SafeMapString.find( pKey );//漂沥 虐蔼狼 iterator 掘扁
MAP_STRING::iterator v_end= m_SafeMapString.end();//End iterator 掘扁
UnLock();
// Data啊 绝绰 版快 贸府
if (v == v_end )
{
return NULL;
}
//iterator甫 烹秦 单捞磐 掘扁
// void *inst = ;
return v->second;
}
void SafeMap::Clear()
{
Lock();
m_SafeMap.clear();
m_SafeMapString.clear();
UnLock();
}
map 多线程 堆栈溢出
[解决办法]
AzitManager::AzitManager()
{
m_MapAzit.Clear();
//这里调用插入不会发生错误
m_MapAzit.Insert(1,(void*)15);
//////////////////////////////
......
........
void AzitManager::CreateAzit(int pAzitHandle)
{
Azit *pAzit = new Azit( true );
m_MapAzit.Insert( pAzitHandle, pAzit );
}