-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMemoryCheck.cpp
70 lines (57 loc) · 1.71 KB
/
MemoryCheck.cpp
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
#include "StdAfx.h"
#include <cassert>
#include "PlatformDepends.h"
#if defined(_DEBUG) || defined(DEBUG)
namespace Scan
{
namespace
{
class VcCrtDebugFlagSeter
{
public:
VcCrtDebugFlagSeter()
{
_CrtSetDbgFlag(
_CRTDBG_ALLOC_MEM_DF |
_CRTDBG_LEAK_CHECK_DF |
_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
}
};
VcCrtDebugFlagSeter g_setter;
}
static ISyncObject* getMemoryCheckSyncObject()
{
static Windows::CriticalSection ls_cs;
return &ls_cs;
}
static const int SCAN_MEMORY_BLOCK = (23 << 16) | _CLIENT_BLOCK;
void* allocMemoryCheck(size_t memSize, const char *fileName, unsigned int line)
{
SingleLocker locker(getMemoryCheckSyncObject());
void* p = _malloc_dbg(memSize, SCAN_MEMORY_BLOCK, fileName, line);
assert(p != NULL);
return p;
}
void freeMemoryCheck(void* memChunk, const char *fileName, unsigned int line)
{
SingleLocker locker(getMemoryCheckSyncObject());
_free_dbg(memChunk, SCAN_MEMORY_BLOCK);
}
}
void* operator new (size_t memSize, const char *fileName, unsigned int line)
{
return Scan::allocMemoryCheck(memSize, fileName, line);
}
void* operator new[] (size_t memSize, const char *fileName, unsigned int line)
{
return Scan::allocMemoryCheck(memSize, fileName, line);
}
void operator delete (void* memChunk, const char *fileName, unsigned int line)
{
Scan::freeMemoryCheck(memChunk, fileName, line);
}
void operator delete[] (void* memChunk, const char *fileName, unsigned int line)
{
Scan::freeMemoryCheck(memChunk, fileName, line);
}
#endif