zoukankan      html  css  js  c++  java
  • [h5棋牌项目]-11-内存复用导致 map 数据重复

    
    //红包结构体
    struct tagRedPacket {
    	tagRedPacket() { ReSet(); }
    	void ReSet() { ZeroMemory(this, sizeof(tagRedPacket)); }
    	LONGLONG		 lTime;									//发红包的时间
    	WORD			 wId;									//红包id
    	DWORD			 dwUserId;								//发红包的用户id
    	DWORD			 dwCellScore;							//红包金额
    	BYTE			 cbRay;									//雷
    	DWORD			 dwRayCompensation;						//中雷赔偿金额
    	BYTE			 cbRayCount;							//中雷次数
    	BYTE			 cbTimeOut;								//过期标识 
    	BYTE			 cbCount;								//红包数量
    	CMD_S_PlayerInfo PlayerInfo;							//发送玩家信息
    	tagOpenRedPacket wRedPacketUser[GAME_PLAYER];			//开红包数组
    }; 
    
    std::map<WORD, tagRedPacket*> m_RedPacketMgr;
    
    tagRedPacket * RedPacketMgr::NewRedPacket()
    {
    	m_nMaxId++;
    	for (std::map<WORD,tagRedPacket*>::iterator it = m_RedPacketMgr.begin(); it != m_RedPacketMgr.end();++it)
    	{
    		if (NULL == it->second) {
    			m_RedPacketMgr.erase(it);
    			break;
    		}
    		if(it->second->cbTimeOut){
    			it->second->ReSet();
    			it->second->wId = m_nMaxId;
    			return it->second;
    		}
    	}
    	tagRedPacket * pRedPacket = new tagRedPacket();
    	pRedPacket->wId = m_nMaxId;
    	return pRedPacket;
    }
    
    void RedPacketMgr::AddRedPacket(tagRedPacket *RedPacket)
    {
    	if(NULL == RedPacket)
    		return;
    	if (m_RedPacketMgr.find(RedPacket->wId) != m_RedPacketMgr.end())
    		return;
    	m_RedPacketMgr[RedPacket->wId] = RedPacket;
    }
    
    这样的代码 m_RedPacketMgr会出现包id重复
    
    改下面这样
    tagRedPacket * RedPacketMgr::NewRedPacket()
    {
    	m_nMaxId++;
    	for (std::map<WORD,tagRedPacket*>::iterator it = m_RedPacketMgr.begin(); it != m_RedPacketMgr.end();++it)
    	{
    		tagRedPacket* ptagRedPacket = it->second;
    		if (NULL == ptagRedPacket) {
    			m_RedPacketMgr.erase(it);
    			break;
    		}
    		//过期的红包
    		if (ptagRedPacket->cbTimeOut) {
    			m_RedPacketMgr.erase(it);
    			delete ptagRedPacket;
    			ptagRedPacket = NULL;
    			break;
    		}
    	}
    	tagRedPacket * pRedPacket = new tagRedPacket();
    	pRedPacket->wId = m_nMaxId;
    	return pRedPacket;
    }
  • 相关阅读:
    阿里云与物理服务器
    ## 100个网路基础知识##
    Linux 中vim编辑器
    Linux 目录结构及增删改查
    Linux 命令行常用快捷键
    XSS劫持cookie登录
    Tomcat
    centos 6.5 搭建DHCP实验
    centos 6.5 系统故障分析实验
    LVM的创建及管理
  • 原文地址:https://www.cnblogs.com/byfei/p/14104143.html
Copyright © 2011-2022 走看看