예제 #1
0
 ScopedSuppress(void) { original = suppressed(); suppress(true); }
예제 #2
0
void CMemLeakDetect::dumpMemoryTrace()
{
    POSITION            pos;
    LPVOID              addr;
    AllocBlockInfo      ainfo;
    TCHAR               buf[MLD_MAX_NAME_LENGTH];
    TCHAR               symInfo[MLD_MAX_NAME_LENGTH];
    TCHAR               srcInfo[MLD_MAX_NAME_LENGTH];
    int                 totalSize                       = 0;
    int                 numLeaks                        = 0;
    CallStackFrameEntry*    p                               = 0;

    //
    _tcscpy_s(symInfo, MLD_MAX_NAME_LENGTH, MLD_TRACEINFO_NOSYMBOL);
    _tcscpy_s(srcInfo, MLD_MAX_NAME_LENGTH, MLD_TRACEINFO_NOSYMBOL);
    //
    pos = m_AllocatedMemoryList.GetStartPosition();
    //
    
    std::string outputFileBase = "MemLeakDetect-" + boost::lexical_cast<std::string>(GetCurrentProcessId()) + "-";

    int n = 0;
    std::string candidate = outputFileBase + boost::lexical_cast<std::string>(++n) + ".txt";
    boost::filesystem::path outputPath = m_outputFolder / candidate;
    while ( boost::filesystem::exists(outputPath) )
    {
        candidate = outputFileBase + boost::lexical_cast<std::string>(++n) + ".txt";
        outputPath = m_outputFolder / candidate;
    }
    m_outputPath = outputPath.file_string();
    m_output.open(m_outputPath.c_str());

    int reported = 0;
    
    int suppressedTotal = 0;

    std::vector<int> suppressed( m_funcsToIgnore.size() );

    while(pos != m_AllocatedMemoryList.end())
    {
        std::basic_string<TCHAR> stackTrace;

        stackTrace += "*****************************************************\n";

        numLeaks++;
        _stprintf_s(buf, _T("Memory Leak(%d)------------------->\n"), numLeaks);
        stackTrace += buf;

        //
        m_AllocatedMemoryList.GetNextAssoc(pos, (LPVOID &) addr, (AllocBlockInfo&) ainfo);
        if (ainfo.fileName[0] != NULL)
        {
            _stprintf_s(buf, _T("Memory Leak <0x%X> bytes(%d) occurance(%d) %s(%d)\n"), 
                    ainfo.address, ainfo.size, ainfo.occurance, ainfo.fileName, ainfo.lineNumber);
        }
        else
        {
            _stprintf_s(buf, _T("Memory Leak <0x%X> bytes(%d) occurance(%d)\n"), 
                    ainfo.address, ainfo.size, ainfo.occurance);
        }
        //
        stackTrace += buf;
        //
        p = &ainfo.traceinfo[0];
        
        stackTrace += ainfo.toString();

        stackTrace += "*****************************************************\n";

        totalSize += ainfo.size;

        bool suppress = false;
        for (std::size_t i=0; i<m_funcsToIgnore.size(); ++i)
        {
            if (stackTrace.find( m_funcsToIgnore[i] ) != std::basic_string<TCHAR>::npos)
            {
                suppress = true;
                ++suppressed[i];
                break;
            }
        }
        
        if (suppress)
        {
            ++suppressedTotal;
        }
        else
        {
            ++reported;
            writeOutput(stackTrace.c_str());
        }
    }

    _stprintf_s(buf, _T("\n-----------------------------------------------------------\n"));
    writeOutput(buf);
    if(!totalSize) 
    {
        _stprintf_s(buf,_T("No Memory Leaks Detected for %d Allocations\n\n"), memoccurance);
        writeOutput(buf);
    }
    else
    {
        _stprintf_s(buf, _T("Total %d Memory Leaks: %d bytes Total Allocations %d\n\n"), numLeaks, totalSize, memoccurance);
        writeOutput(buf);

        _stprintf_s(buf, _T("Suppressed Leaks: %d, Reported Leaks: %d.\n\n"), suppressedTotal, reported);
        writeOutput(buf);

        std::ostringstream os;
        os << "Suppressed Leaks: " << std::endl;
        for (std::size_t i=0; i<m_funcsToIgnore.size(); ++i)
        {
            os << m_funcsToIgnore[i] << " (" << suppressed[i] << ")" << std::endl;
        }
        writeOutput(os.str().c_str());
    }

    m_output.close();
}