1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* Copyright (c) MediaArea.net SARL. All Rights Reserved.
*
* Use of this source code is governed by a zlib-style license that can
* be found in the License.txt file in the root of the source tree.
*/
//---------------------------------------------------------------------------
#include "ZenLib/PreComp.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include "ZenLib/Conf_Internal.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#if defined(ZENLIB_DEBUG)
//---------------------------------------------------------------------------
#include <iomanip>
#include <sstream>
#include "ZenLib/MemoryDebug.h"
#include "ZenLib/Ztring.h"
#ifdef WINDOWS
#include <io.h>
#else
#include <cstdio>
#endif
#include <fcntl.h>
#include <sys/stat.h>
using namespace std;
//---------------------------------------------------------------------------
namespace ZenLib
{
bool MemoryDebug::g_IsShutdown = false;
//***************************************************************************
// Constructors/destructor
//***************************************************************************
MemoryDebug::MemoryDebug()
{
}
MemoryDebug::~MemoryDebug()
{
if (!m_Blocks.empty())
ReportLeaks();
g_IsShutdown = true;
}
//***************************************************************************
// Instance
//***************************************************************************
MemoryDebug& MemoryDebug::Instance()
{
static MemoryDebug Inst;
return Inst;
}
//***************************************************************************
// Reports
//***************************************************************************
void MemoryDebug::ReportLeaks()
{
Ztring m_File;
//std::ofstream m_File ("Debug_MemoryLeak.txt"); // Fichier de sortie
// Dtail des fuites
std::size_t TotalSize = 0;
for (TBlockMap::iterator i = m_Blocks.begin(); i != m_Blocks.end(); ++i)
{
// Ajout de la taille du bloc au cumul
TotalSize += i->second.Size;
// Inscription dans le fichier des informations sur le bloc courant
/*
m_File << "-> 0x" << std::hex << i->first << std::dec
<< " | " << std::setw(7) << std::setfill(' ') << static_cast<int>(i->second.Size) << " bytes"
<< " | " << i->second.File.c_str() << " (" << i->second.Line << ")" << std::endl;
*/
m_File.append(__T("-> 0x"));
m_File.append(Ztring::ToZtring((size_t)i->first, 16));
m_File.append(__T(" | "));
Ztring Temp;
Temp.From_Number(static_cast<int>(i->second.Size));
while(Temp.size()<7)
Temp=__T(" ")+Temp;
m_File.append(Temp);
m_File.append(__T(" bytes"));
m_File.append(__T(" | "));
m_File.append(Ztring().From_Local(i->second.File.c_str()));
m_File.append(__T(" ("));
m_File.append(Ztring::ToZtring(i->second.Line));
m_File.append(__T(")"));
m_File.append(EOL);
}
// Affichage du cumul des fuites
/*
m_File << std::endl << std::endl << "-- "
<< static_cast<int>(m_Blocks.size()) << " non-released blocs, "
<< static_cast<int>(TotalSize) << " bytes --"
<< std::endl;
*/
m_File.append(EOL);
m_File.append(EOL);
m_File.append(__T("-- "));
m_File.append(Ztring::ToZtring(static_cast<int>(m_Blocks.size())));
m_File.append(__T(" non-released blocs, "));
m_File.append(Ztring::ToZtring(static_cast<int>(TotalSize)));
m_File.append(__T(" bytes --"));
m_File.append(EOL);
std::string ToWrite=m_File.To_Local().c_str();
int m_File_sav=open("Debug_MemoryLeak.txt", O_BINARY|O_RDWR |O_CREAT); // Fichier de sortie
write(m_File_sav, (int8u*)ToWrite.c_str(), ToWrite.size());
close(m_File_sav);
}
//***************************************************************************
// Memory management
//***************************************************************************
void* MemoryDebug::Allocate(std::size_t Size, const char* File, int Line, bool Array)
{
// Allocation de la mmoire
void* Ptr = malloc(Size);
// Ajout du bloc la liste des blocs allous
TBlock NewBlock;
NewBlock.Size = Size;
NewBlock.File = File;
NewBlock.Line = Line;
NewBlock.Array = Array;
m_Blocks[Ptr] = NewBlock;
return Ptr;
}
void MemoryDebug::Free(void* Ptr, bool Array)
{
// Recherche de l'adresse dans les blocs allous
TBlockMap::iterator It = m_Blocks.find(Ptr);
// Si le bloc n'a pas t allou, on gnre une erreur
if (It == m_Blocks.end())
{
// En fait a arrive souvent, du fait que le delete surcharge est pris en compte meme la ou on n'inclue pas DebugNew.h,
// mais pas la macro pour le new
// Dans ce cas on dtruit le bloc et on quitte immdiatement
free(Ptr);
return;
}
// Si le type d'allocation ne correspond pas, on gnre une erreur
if (It->second.Array != Array)
{
//throw CBadDelete(Ptr, It->second.File.c_str(), It->second.Line, !Array);
}
// Finalement, si tout va bien, on supprime le bloc et on loggiz tout a
m_Blocks.erase(It);
m_DeleteStack.pop();
// Libration de la mmoire
free(Ptr);
}
void MemoryDebug::NextDelete(const char* File, int Line)
{
TBlock Delete;
Delete.File = File;
Delete.Line = Line;
m_DeleteStack.push(Delete);
}
//***************************************************************************
//
//***************************************************************************
} //NameSpace
#endif // defined(ZENLIB_DEBUG)
|