zoukankan      html  css  js  c++  java
  • _CrtDumpMemoryLeaks报告程序中的内存泄露问题(简单示例代码)

    // .h 文件  
    #pragma once  
    class CConsoleDump  
    {  
    public:  
        explicit CConsoleDump(LPCTSTR lpszWindowTitle = NULL);  
        virtual ~CConsoleDump(void);  
      
    public:  
        BOOL DUMP(LPCTSTR lpszFmt, ...);  
        BOOL ShowWindow(BOOL bShowWindow);  
        BOOL SetWindowText(LPCTSTR lpszWindowTitle = NULL);  
    };  
      
    // .cpp文件  
    #include "StdAfx.h"  
    #include "ConsoleDump.h"  
      
    #define MAX_BUFFER_SIZE (10 * 1024)  
      
    CConsoleDump::CConsoleDump(LPCTSTR lpszWindowTitle)  
    {  
        if(AllocConsole())  
        {  
            if(NULL != lpszWindowTitle)  
            {  
                SetConsoleTitle(lpszWindowTitle);  
            }  
        }  
    }  
      
    CConsoleDump::~CConsoleDump(void)  
    {  
        FreeConsole();  
    }  
      
    BOOL CConsoleDump::ShowWindow(BOOL bShowWindow)  
    {  
        return ::ShowWindow(GetConsoleWindow(), bShowWindow ? SW_SHOW : SW_HIDE);  
    }  
      
    BOOL SetWindowText(LPCTSTR lpszWindowTitle)  
    {  
        if(NULL != lpszWindowTitle)  
        {  
            return SetConsoleTitle(lpszWindowTitle);  
        }  
        return TRUE;  
    }  
      
    BOOL CConsoleDump::DUMP(LPCTSTR lpszFmt, ...)  
    {  
        TCHAR szText[MAX_BUFFER_SIZE] = {0};  
      
        va_list arglist;  
        va_start(arglist, lpszFmt);  
        _vstprintf_s(szText, _countof(szText), lpszFmt, arglist);  
        va_end(arglist);  
      
        return WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szText, _tcslen(szText), NULL, NULL);  
    }  
      
    // 测试使用  
    CConsoleDump m_dump; // 定义为类的成员变量  
    // 需要的地方利用CConsoleDump::DUMP函数输出log信息即可  
    m_dump.DUMP(_T("Hello, World!
    ")); 

    这只是个简单的封装了Console相关的几个函数,关于更多的Console相关的控制,可以参考MSDN文档中的

    参考:http://blog.csdn.net/visualeleven/article/details/7628564

  • 相关阅读:
    VC字符串输出对齐问题(转)
    木马免杀全攻略(转)
    Windows Vista自动重启问题解决方法(转)
    图说VSS 6.0构架版本控制系统解决方案(转)
    几个有用的链接
    X64 Windows 2003 及XP 语言包官方下载
    .NET 3.5的版本问题(转)
    设计模式读书笔记工厂方法模式
    设计模式读书笔记装饰者模式
    设计模式读书笔记简单工厂模式
  • 原文地址:https://www.cnblogs.com/findumars/p/4847425.html
Copyright © 2011-2022 走看看