Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# MemoryChecker.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * メモリチェッカ実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Core/Debug/MemoryChecker.h"
    00027 #include "Core/Debug/DebugOutput.h"
    00028
    00029 // デバッグ時のみ機能します。
    00030 #ifdef _DEBUG
    00031
    00032 namespace Lamp{
    00033
    00034 // 初期化フラグ
    00035 bool MemoryChecker::initialized_ = false;
    00036
    00037 //------------------------------------------------------------------------------
    00038 // メモリチェッカ初期化
    00039 void MemoryChecker::initialize(){
    00040     if(initialized_){ return; }
    00041     // プログラム終了時にメモリリークチェック。
    00042     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    00043     // ワーニング、エラー、アサートをデバッグ出力に出す。
    00044     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
    00045     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
    00046     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
    00047     initialized_ = true;
    00048 }
    00049 //------------------------------------------------------------------------------
    00050 // ステートの取得
    00051 MemoryChecker::State MemoryChecker::getState(){
    00052     State state;
    00053     _CrtMemCheckpoint(&state.state);
    00054     return state;
    00055 }
    00056 //------------------------------------------------------------------------------
    00057 // 詳細ダンプ
    00058 void MemoryChecker::dumpDetail(){
    00059     _CrtMemDumpAllObjectsSince(NULL);
    00060 }
    00061 //------------------------------------------------------------------------------
    00062 // 詳細の差分ダンプ
    00063 void MemoryChecker::dumpDetail(const State& oldState){
    00064     _CrtMemDumpAllObjectsSince(&oldState.state);
    00065 }
    00066 //------------------------------------------------------------------------------
    00067 // 統計のダンプ
    00068 void MemoryChecker::dumpStatistics(){
    00069     State state;
    00070     _CrtMemCheckpoint(&state.state);
    00071     _CrtMemDumpStatistics(&state.state);
    00072 }
    00073 //------------------------------------------------------------------------------
    00074 // 統計の差分ダンプ
    00075 void MemoryChecker::dumpStatistics(const State& oldState){
    00076     State newState, diffState;
    00077     _CrtMemCheckpoint(&newState.state);
    00078     _CrtMemDifference(&diffState.state, &oldState.state, &newState.state);
    00079     _CrtMemDumpStatistics(&diffState.state);
    00080 }
    00081 //------------------------------------------------------------------------------
    00082 // メモリリークチェック
    00083 bool MemoryChecker::leakCheck(const State& oldState){
    00084     State newState, diffState;
    00085     _CrtMemCheckpoint(&newState.state);
    00086     if(_CrtMemDifference(&diffState.state, &oldState.state, &newState.state)){
    00087         DebugOutThickLine();
    00088         DebugOut("!!! メモリリーク !!!\n");
    00089         DebugOutLine();
    00090         _CrtMemDumpAllObjectsSince(&diffState.state);
    00091         DebugOutLine();
    00092         _CrtMemDumpStatistics(&diffState.state);
    00093         DebugOutLine();
    00094         DebugOut("!!! メモリリーク !!!\n");
    00095         DebugOutThickLine();
    00096         return true;
    00097     }
    00098     return false;
    00099 }
    00100 //------------------------------------------------------------------------------
    00101 } // End of namespace Lamp
    00102 #endif // End of _DEBUG
    00103 //------------------------------------------------------------------------------