2 pckr315 pckr315 于 2016.02.29 08:46 提问

共享内存 内存泄露问题

我用的是dll, 一个进程WriteFileMap写入,另一个进程ReadFileMap,WriteFileMap每写入一次内存就增加大概1兆,知道报错〖8〗-存储空间不足,无法处理此命令。求大神,是不是哪里写的有问题
MT4_EXPFUNC wchar_t* __stdcall WriteFileMap(const wchar_t *strdat,const int anumber)
{

    char DataBuf[983040];
    char cnumber[20];
    char error[10];
    itoa(anumber,cnumber,sizeof(cnumber));
    w2c(strdat,DataBuf,sizeof(DataBuf));

    //if(!MapWriteing && !MapReading)
            m_WriteMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, LPCTSTR(cnumber));
        if (m_WriteMap == NULL) 
            m_WriteMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 983040,  LPCTSTR(cnumber));
        //  m_WriteMap = CreateFileMapping(0xFFFFFFFFFFFFFFFF, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 983040,  LPCTSTR(cnumber));
        if (m_WriteMap == NULL) 
            {
                    _ultoa( GetLastError(), error, 10 );
                    MapWriteing=0;
                    return(c2w(error));
            }
            m_lpWriteBuf = (LPBYTE)MapViewOfFile(m_WriteMap, FILE_MAP_WRITE, 0, 0, 0);
            if (m_lpWriteBuf == NULL)
                {
                    CloseHandle(m_WriteMap);
                    m_WriteMap = NULL;
                    _ultoa( GetLastError(), error, 10 );
                    MapWriteing=0;
                    return(c2w(error));
                }
            else
                memcpy(m_lpWriteBuf,DataBuf,sizeof(DataBuf));
        // 不需要内存映射的时候要关闭,两边都需要关闭内存映射文件

        if (m_WriteMap != NULL)
        {
        UnmapViewOfFile(m_WriteMap);
        m_WriteMap = NULL;
        }   
            if (m_lpWriteBuf != NULL)
        {
        CloseHandle(m_lpWriteBuf);
        m_lpWriteBuf = NULL;
        }

        return(c2w("1"));       
}    

//宿主进程读取共享数据;
MT4_EXPFUNC wchar_t* __stdcall ReadFileMap(const int anumber)
{

char DataBuf[983040];
char cnumber[20];
char error[10];
itoa(anumber,cnumber,sizeof(cnumber));

        m_ReadMap = OpenFileMapping(FILE_MAP_READ, FALSE, LPCTSTR(cnumber));
        //m_ReadMap = OpenFileMapping(FILE_MAP_READ, FALSE, "1634061");
        if (m_ReadMap == NULL) 
            {
            _ultoa( GetLastError(), error, 10 );
            MapReading=0;
            return(c2w(cnumber));
        //  return(c2w("err1"));
            }
        m_lpReadBuf = (LPBYTE)MapViewOfFile(m_ReadMap,FILE_MAP_READ,0,0,0);
        if (m_lpReadBuf == NULL)
        {
            CloseHandle(m_ReadMap);
            m_ReadMap=NULL;
            _ultoa( GetLastError(), error, 10 );
            MapReading=0;
            return(c2w(error));
        //  return(c2w("err2"));
        }
        else
            memcpy(DataBuf,m_lpReadBuf,sizeof(DataBuf));

            // 不需要内存映射的时候要关闭,两边都需要关闭内存映射文件
            if (m_lpReadBuf != NULL)
            {
            UnmapViewOfFile(m_lpReadBuf);
            m_lpReadBuf = NULL;
            }       /*
            if (m_ReadMap != NULL)
            {
            CloseHandle(m_ReadMap);
            m_ReadMap = NULL;
            }
            MapReading=0;*/
        //  return(c2w("err3"));
            return(c2w(DataBuf));

}

1个回答

hikarisamax
hikarisamax   2016.02.29 17:15

不妨

char DataBuf[983040] 改为 char* DataBuf = new char[983040];
返回处别忘了delete[] DataBuf;

在代码段用硬代码分配大长度的字符数组是很危险的,很容易泄露内存地址.

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!