首页
社区
课程
招聘
内存泄露求PATCH
发表于: 2015-11-27 10:42 5095

内存泄露求PATCH

2015-11-27 10:42
5095
CString GetText(CString strURL)
{
       
       
        CInternetSession session;  
        CHttpFile *file = NULL;  
        CString strHtml = "";   //存放网页数据  
       
        try{  
                file = (CHttpFile*)session.OpenURL(strURL);  
               
        }catch(CInternetException * m_pException){  
                file = NULL;  
                m_pException->m_dwError;  
                m_pException->Delete();  
                session.Close();  
                printf("CInternetException");  
               
        }  
        CString strLine;  
        if(file != NULL){  
               
                while(file->ReadString(strLine) != NULL){  
                        strHtml += strLine;  
                }  
               
               
               
        }else{  
                printf("fail");  
        }  
       
        session.Close();  
        file->Close();  
        delete file;  
        file = NULL;  
//        printf("%s\r\n",strHtml.GetBuffer(0));
        return strHtml;
       
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 112
活跃值: (57)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
2
CString GetText(CString strURL);
void CTestMfc1126Dlg::OnButton1()
{
        // TODO: Add your control notification handler code here
        GetText("http://bbs.pediy.com/showthread.php?t=206092");
}

CString GetText(CString strURL)
{
    char* pBuf = new char[0x10]; ///< 这里制造一个内存泄露
    /**
    Detected memory leaks!
    Dumping objects ->
    D:\temp-ls\testMfc1126\testMfc1126Dlg.cpp(184) : {94} normal block at 0x00571648, 16 bytes long.
    Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    Object dump complete.
    */

    /// 当程序退出时, 在IDE中可以看到pBuf有内存泄露, 但是下面的代码没有内存泄露
    /// 是和URL有关系么? 你访问的是啥URL?
    /// 我怀疑是你访问的url失败时, 导致没有执行资源的释放引起的内存泄露
    /// 另外, 你看内醋泄漏的方法是什么?怎么看的?

    /// 下面的代码没有内存泄露
   
   
    CInternetSession session;  
    CHttpFile *file = NULL;  
    CString strHtml = "";   //存放网页数据  
   
    try{  
        file = (CHttpFile*)session.OpenURL(strURL);  
        
    }catch(CInternetException * m_pException){  
        file = NULL;  
        m_pException->m_dwError;  
        m_pException->Delete();  
        session.Close();  
        printf("CInternetException");  
        
    }  
    CString strLine;  
    if(file != NULL){  
        
        while(file->ReadString(strLine) != NULL){  
            strHtml += strLine;  
        }  
        
        
        
    }else{  
        printf("fail");  
    }  
   
    ///< 这么写有BUG, 如果给定一个url失败了, from session.OpenURL
    /// 这里就会报错!
    session.Close(); ///< ! 要额外处理一下失败的情况
    file->Close();  
    delete file;  
    file = NULL;  
    //  printf("%s\r\n",strHtml.GetBuffer(0));
    return strHtml;
   
}
2015-11-27 11:36
0
雪    币: 112
活跃值: (57)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
3
是不是应该先执行 file->close(), 再执行 session.close()?
2015-11-27 11:38
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
无论如何,先谢谢SilentGamb了!
2015-11-28 17:00
0
游客
登录 | 注册 方可回帖
返回
//