, , . , . , , . ++-, . - ( DCRT) Microsoft.
Visual Basic , Visual Basic . Java (JVM garbage collector), , Java . , , , Java- , , .
C++ . . , - ( CRT) , Microsoft Visual C++ 4, CRT (DCRT).
DCRT Debug Run-Time library. .
, C++, , , DCRT . "" , . , , , DCRT. DCRT : MemDumperValidator MemStress, .
DCRT- , (heap). , /++-, new, maiioc calloc. (underwrites), , (overwrites), . ( ). , MFC, , , DCRT, MFC .
Overwrite . (program writes past the end of block memory), : , , -, .
DCRT ( __RPTn RPTFn, ). DCRT 3, , . , DCRT , , . SUPERASSERT, BUGSLAYERUTIL.DLL.
DCRT- . . 15.1 , DCRT-. CRT Microsoft Visual Studio ( ), CRT DCRT ..\Microsoft Visual Studio\VC98\CRT\SRC.
15.1. DCRT-.
|
|
DBGDEL.CPP |
delete |
DBGHEAP.C |
|
DBGHOOK.C |
|
DBGINT.H |
|
DBGNEW.CPP |
new |
DBGRPT.C |
|
CRTDBG.H |
, , DCRT-. \lnclude |
DCRT- , . #inciude ( ) :
#define _CRTDBG_MAP_ALLOC
CRTDBG.H. _CRTDBG_MAP_ALLOC , .
DCRT- . , DCRT- . , , . (release build, ), (debug build) ! . , , , . _CrtsetDbgFiag , . 15.2. DCRT, .
#inciude #define _crtsetDbgFlag DCRT, . , , .
DCRT _CrtcheckMemory. , - , . - DCRT !
. _CrtIsValidHeapPointer, _CrtIsMemoryBlock _crtisVaiidPointer . _crtcheckMemory .
15.2. DCRT
|
|
_CRTDBG_ALLOC_MEM_DF |
. , |
_CRTDBG_CHECK_ALWAYS_DF |
(). , |
_CRTDBG_CHECK_CRT_DF |
CRT . CRT , CRT-. , , |
CRTDBG_DELAY_FREE_MEM_DF |
. OxDD, , , . |
(.) |
, . , OxDD DCRT , . , , , |
_CRTDBG_LEAK_CHECK_DF |
. |
DCRT _CrtMemCheckpoint, _CrtMemDifference _CrtMemDumpStatistics. , . , ( ) CRT-, , , .
DCRT , . TRUE, . FALSE, , . , , ( ). MemStress ( BUGSLAYERUTIL.DLL). , , DCRT- ( ).
, DCRT (. . , ). , "" . , (. . ), ( , , , ), . MFC Dump, , cobject. ( ) MFC, , .
, , . . , MemDumperVaiidator BUGSLAYERUTIL.DLL, , . ( underwrites overwrites). , , , , .
CRT- Windows , , . , : (DCRT) (CRT). , (DLL).
CRT- . --, . , DLL . , , . CRT . DLL CRT-, ( DLL, CRT- ).
DLL- CRT MSVCRT(D).DLL . DLL , , , , .
DLL- CRT, DLL , . DLL- , DLL, DLL.
CRT-, , . DLL, CRT-, , , . DLL, , CRT, , DLL, , . , , . . CRT-, , , .
DLL- CRT . . , , , CRT-, , .
BUGSLAYERUTIL.DLL DLL- CRT. , BUGSLAYERUTIL.DLL DCRT-: MemDumperValidator MemStress, . , DLL- . , , DLL-, MEMDUMPERVALIDATOR.CPP, MEMDUMPERVALIDATOR.H, MEMSTRESS.CPP, MEMSTRESSCONSTANTS.H MEMSTRESS.H, .
BUGSLAYERUTIL.DLL. , . , MemDumperValidator DCRT--, _CRTDBG_CHECK_ALWAYS_DF. , , , DCRT- . , . , . _CRTDBG_CHECK _ALWAYS_DF ( _GrtsetDbgFiag). , ,
.
MemDumperValidator DCRT- . DCRT- , ( underwrites overwrites) . , , , , :
Detected memory leaks
Dumping objects ->
:\vc\INCLUDE\crtdbg.h(552) : (596} normal block at Ox008CD5BO,
24 bytes long.
Data: < k w k > 90 6B 8C 00 BO DD 8C 00 00 00 80 77 90 6 8 00
Object dump complete.
, , , . ( ). , MemDumperValidator, . , .
MemDumperValidator DCRT-, , - . , DCRT, , . 15.3. DCRT: _nh_maiioc_ dbg (new), _malloc_dbg (malloc), _calloc_dbg (calloc) _realloc_dbg (realloc).
15.3.
_NORMAL_BLOCK |
(new, malloc calloc) .
, , |
_CRT_BLOCK |
, , CRT-, (, ). , . , |
CLIENT BLOCK |
, , , CLIENT BLOCK VALUE (. heap alloc dbg #define). , 1 6- 16 , :
(. . , ) - ( CrtSetDumpClient). - , DCRT- - |
(.) |
. , (__CrtDoForAlldientObjects), , . MFC cobject. MemDumperValidator |
FREE BLOCK |
. CrtSetDbgFlag CRTDBG DELAY FREE MEM DF, , OxDD |
IGNORE BLOCK |
DCRT-, , , Ignore |
MemDumperValidator : ( C++) ( ), DCR- MemDumperValidator , . -, . , . , DCRT- , .
MemDumperValidator , . 15.1 MEMDUMPERVALIDATOR.H, . BUGSLAYERUTIL.H, MEMDUMPERVALIDATOR.H.
15-1, JWEMDUMPERVAL1DATOR.H
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Debugging Applications" (Microsoft Press)
Copyright () 1997-2000 John Robbins All rights reserved.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifndef _MEMDUMPERVALIDATOR_H
#define _MEMDUMPERVALIDATOR_H
//He ; BUGSLAYER.H
#ifndef _BUGSLAYERUTIL_H
#error "Include BUGSLAYERUTIL.H instead of this file directly!"
#endif // _BUGSLAYERUTIL_H
// CRTDBG.H.
#include "MSJDBG.h"
#ifdef _cplusplus
extern "C" {
#endif // _ _cplusplus
// .
#ifdef _DEBUG
/////////////////////////////////////////////////////////////
// typedef
////////////////////////////////////////////////////////////////
// . -
// .
// , ,
// , ,
// DCRT-.
typedef void (*PFNMEMDUMPER)(const void *);
// (validating function).
// ,
// ,
// , ValidateAllBlocks function.
typedef void (*PFNMEMVALIDATOR)(const void *, const void *);
////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////
// , Client-.
//
// dwValue DVINFO (. ).
tdefine CLIENT_BLOCK_VALUE(x) (_CLIENT_BLOCK|(x«16))
//
Idefine CLIENT_BLOCK_SUBTYPE() (( » 16) & 0xFFFF)
/////////////////////////////////////////////////////////////
// ,
// Client-
////////////////////////////////////////////////////////////
typedef struct tag_DVINFO
{
// Client-.
// .
// CLIENT_BLOCK_VALUE. ,
// , . AddClientDV.
unsigned long dwValue ;
//
PFNMEMDUMPER pfnDump
//
PFNMEMVALIDATOR pfnValidate;
} DVINFO, * LPDVINFO;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: AddClientDV
:
Client-. dwValue DVINFO ,
.
_malloc_dbg
Client-.
CLIENT_BLOCK__VALUE, ,
_malloc_dbg.
, .
?
,
.
:
IpDVInfo DVINFO
:
1
.
0
.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int BUGSUTIL_DLLINTERFACE _stdcall AddClientDV (LPDVINFO IpDVInfo);
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: ValidateAllBlocks
:
.
, Client-
.
, VALIDATEALLBLOCKS, .
:
pContext ,
.
:
.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void BUGSOTIL_DLLINTERFACE _stdcall
ValidateAllBlocks ( void * pContext);
#ifdef _cplusplus
//////////////////////////////////////////////////////////////
// C++
//////////////////////////////////////////////////////////////
// MFC-,
#define DECLAREJYEMDEBUG(classname)
public :
static DVINFO m_stDVInfo;
static void ClassDumper ( const void * pData);
static void ClassValidator ( const void * pData,
const void * pContext);
static void * operator new ( size_t nSize)
{
if ( 0 == m_stDVInfo.dwValue)
{
m_stDVTnf.pfnDump = classname::ClassDumper;
m_stDVInfo.pfnValidate = classname::ClassValidator;
AddClientDV ( &m_stDVInfo);
}
return ( _malloc_dbg ( nSize
(int)m_stDVlnf.dwValue,
_FILE_ ,
_LINE_ ) ) ;
}
static void * operator new ( size_t nSize ,
char * IpszFileName,
int nLine )
{
if ( 0 = m_stDVInfo.dwValue)
{
m_stDVInfo.pfnDump = classname::ClassDumper;
m_stDVInfo.pfnValidate = classname::ClassValidator;
AddClientDV ( &m_stDVInfo);
}
return ( _malloc_dbg ( nSize
(int)m_stDVInf.dwValue,
IpszFileName ,
nLine )) ;
}
static void operator delete ( void * pData)
{
_free_dbg ( pData, (int)m_stDVInfo.dwValue);
}
// -.
#define IMPLEMENT_MEMDEBUG(classname)
DVINFO classname::m_stDVInf = { 0, 0, 0 }
// .
// DEBUG_NEW, .
#ifdef DEBUG_NEW
tdefine MEMDEBUG_NEW DEBUG_NEW
#else
#define MEMDEBUG_NEW new ( _FILE_, _LINE_)
#endif
#endif // _cplusplus
//////////////////////////////////////////////////////////////////
// -
/////////////////////////////////////////////////////////////////
// -.
//
// DVINFO.
Idefine INITIALIZE_MEMDEBUG(IpDVInfo, pfnD, pfnV)
{
ASSERT ( FALSE == IsBadWritePtr ( IpDVInfo,
sizeof ( DVINFO)));
((LPDVINFO)IpDVInfo)->dwValue = 0;
((LPDVINFO)IpDVInfo)->pfnDump = pfnD;
((LPDVINFO)IpDVInfo)->pfnValidate = pfnV;
AddClientDV ( IpDVInfo);
}
// , -
// .
// DVINFO,
// .
#define MEMDEBUG_MALLOC(IpDVInfo, nSize) \
_malloc_dbg ( nSize , \
((LPDVINFO)IpDVInfo)->dwValue, \
_FILE_ , \
_LINE_ )
#define MEMDEBUG_REALLOC(IpDVInfo, pBlock, nSize) \
__realloc_dbg ( pBlock , \
nSize , \
((LPDVINFO)IpDVInfo)->dwValue , \
_FILE_ , \
_LINE_ )
#define MEMDEBUG_EXPAND(IpDVInfo, pBlock, nSize) \
_expand_dbg( pBlock , \
nSize , \
' ((LPDVINFO)IpDVInfo)->dwValue , \
_FILE_ , \
_LINE_ )
#define MEMDEBUG_FREE(lpDVInfo, pBlock) \
_free_dbg ( pBlock , \
((LPDVINFO)IpDVInfo)->dwValue)
#define MEMDEBUG_MSIZE(IpDVInfo, pBlock} \
_msize_dbg ( pBlock, ((LPDVINFO)IpDVInfo)->dwValue)
// ValidateAllBlocks
#define VALIDATEALLBLOCKS(x) ValidateAllBlocks ( x)
#else // _DEBUG .
#ifdef _cplusplus
#define DECLARE_MEMDEBUG(classname)
#define IMPLEMENT_MEMDEBUG(classname)
#define MEMDEBUG_NEW new
#endif // _cplusplus
#define INITIALIZE_MEMDEBUG(IpDVInfo, pfnD, pfnV)
#define MEMDEBUG_MALLOC(IpDVInfo, nSize) \
malloc ( nSize)
#define MEMDEBUG_REALLOC(IpDVInfo, pBlock, nSize) \
realloc ( pBlock, nSize)
#define MEMDEBUG_EXPAND(IpDVInfo, pBlock, nSize) \
_expand ( pBlock, nSize)
#define MEMDEBUG_FREE(IpDVInfo, pBlock) \
free ( pBlock) ttdefine MEMDEBUG_MSIZE(IpDVInfo, pBlock) \
_msize ( pBlock)
#define VALIDATEALLBLOCKS(x)
#endif // _DEBUG ttifdef _cplusplus
}
#endif // _cplusplus
#endif // _MEMDUMPERVALIDATOR_H
, MemDumperValidator C++, MemDumperValidator. DECLARE_MEMDEBUG . MFC, . 15-1 new, delete new, . , .
++
IMPLEMENT_MEMDEBUG ( ), .
DECLARE
_MEMDEBUG IMPLEMENT_MEMDEBUG
, .
( ), , . :
static void ClassDumper ( const void * pData);
static void ClassValidator ( const void * pData,
const void * pContext);
, , .
pData () . , pData (. . ), . , (), pData , , . ClassValidator - pContext , ValidateAliBlocks. , " " .
ClassDumper. -, , DCRT-, _RPTn _RFTFn. -, CR/LF (Carriage Return/Line Feed / ), DCRT- .
C++ . ? , - .
? : , , -. , , , .
MemDumperValidator -, . -, DVINFO- , . C++ , . , , , DVINFO-. - , C++, , static . DVINFO- - . 1 MemDumperValidator . MemDumperValidator INITIALIZE_MEMDEBUG, ' DVINFO-, . .
, : // (ailocation/free/reallocation), (dump), (validation), (get/expand block size).
, . , DVINFO- stdvBiockinfo, - :
MEMDEBUG_MALLOC(&stdvBlockInfo, sizeof () ) ;
-, , 15-1. DVINFO- , , DVINFO- -; .
MemDumperValidator, , , : " , "" ?" , "". , . , , , . , . "" (wild) .
, . , , (self-referential data structures) ( ) . , . , , . "" , , , , , . , C++, . VALIDATEALLBLOCKS. vaiidateAHBiocks. , , . , . , validateAHBlocks , .
, MemDumperValidator, Dump, 15-2. Dump "", , ,, . , MemDumperValidator MFC, MFC .
15-2. DUMP.CPP
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Debugging Applications" (Microsoft Press)
Copyright (c) 1997-2000 John Robbins All rights reserved.
- - - - - - - - - - - - - - - - - - - - - - - - - -*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <iostream.h>
#include "BugslayerUtil.h"
class TestClass
{
public:
TestClass ( void)
{
strcpy ( m_szData, "TestClass constructor data!");
}
TestClass ( void)
{
m_szData[ 0 ] = '\0';
}
// C++
DECLARE_MEMDEBUG ( TestClass);
private :
char m_szData[ 100 ];
};
// DVINFO .
IMPLEMENT_MEMDEBUG ( TestClass);
Listing 15-2DUMP.CPP *
//
#ifdef _DEBUG
void TestClass::ClassDumper ( const void * pData)
{
TestClass * pClass = (TestClass*JpData;
_RPT1 ( _CRT_WARN,
" TestClass::ClassDumper : %s\n", pClass->m_szData);
}
void TestClass::ClassValidator ( const void * pData ,
const void * )
{
// .
TestClass * pClass = (TestClass*)pData;
_RPT1 ( _CRT_WARN ,
" TestClass::ClassValidator : %s\n",
pClass->m_szData );
}
#endif
typedef struct tag_SimpleStruct
{
char szNamef 256 ]; char szRank[ 256 ];
}
SimpleStruct;
// ,
//
void DumperOne ( const void * pData}
{
_RPT1 ( _CRT_WARN, " Data is : %s\n", pData);
}
void ValidatorOne ( const void * pData, const void * pContext)
{
//
. _RPT2 ( _CRT_WARN,
" Validator called with : %s : Ox%08X\n",
pData, pContext);
}
//
void DumperTwo ( const void * pData)
{
_RPT2 ( _CRT_WARN
" Data is Name : %s\n"
" Rank : %s\n" ,
((SimpleStruct*)pData)->szName ,
((SimpleStruct*)pData)->szRank );
}
void ValidatorTwo ( const void * pData, const void * pContext)
{
// .
_RPT2 ( _CRT_WARN , " Val%dator called with :\n"
" Data is Name : %s\n"
" Rank : %s\n" ,
((SimpleStruct*)pData)->szName ,
((SimpleStruct*)pData)->szRank );
}
// ,
// DVINFO. ,
// MEMDEBUG -,
static DVINFO g_dvOne;
static DVINFO g_dvTwo;
void main ( void)
{
cout « "At start of main\n";
// One.
INITIALIZE_MEMDEBUG ( &g_dvOne, DumperOne, ValidatorOne) ;
// Two.
INITIALIZE_MEMDEBUG ( Sg_dvTwo, DumperTwo, ValidatorTwo) ;
// MEMDEBUG.
TestClass * pstClass;
//pstClass = MEMDEBUG_NEW TestClass;
pstClass = new TestClass;
// .
char * = (char*)MEMDEBUG_MALLOC '( &g_dvOne, 10);
strcpy ( , "VC VC");
SimpleStruct * pSt =
(SimpleStruct*)MEMDEBUG_MALLOC ( Sg_dvTwo,
sizeof ( SimpleStruct));
strcpy ( pSt->szName, "Pam");
strcpy ( pSt->szRank, "CINC");
// .
VALIDATEALLBLOCKS ( NULL);
cout « "At end of main\n";
//
}
MemDumperValidator . , , , DCRT- , hook- . , , DCRT-. , DCRT- , . _GrtMemBiockHeader, DBGINT.H. , DBGINT.H , _GrtMemBiockHeader , , . , _CrtMemBiockHeader CRTDBG_INTERNALS.H ( 15-3). , , , _crtMemBiockHeader DCRT- Visual C++ 4- 6-. , Visual C++. MemDumperValidator (major release of the compiler), , .
DBGINT.H, CRTDBG_INTERNALS.H #indude DBGINT.H. <VC98>\CRT\SRC INCLUDE, Include- Directories Options IDE Visual C++. CRT-, README.TXT, .
15-3.CRTDBG_INTERNALS.H
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Debugging Applications" (Microsoft Press)
Copyright (c) 1997-2000 John Robbins All rights reserved.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
#ifndef _CRTDBG_INTERNALS_H
#define _CRTDBG_INTERNALS_H
#define nNoMansLandSize 4
typedef struct _CrtMemBlockHeader
{
struct _CrtMemBlockHeader * pBlockHeaderNext ;
struct _CrtMemBlockHeader * pBlockHeaderPrev ;
char * szFileName ;
int nLine ;
size_t nDataSize ;
int nBlockUse ;
long IRequest ;
unsigned char gap[nNoMansLandSize] ;
/* followed by:
* unsigned char data[nDataSize];
* unsigned char anotherGap[nNoMansLandSize];
*/
} _CrtMemBlockHeader;
#define pbData(pblock) ((unsigned char *) \
((_CrtMemBlockHeader *)pblock + 1) )
tfdefine pHdr(pbData) (((_CrtMemBlockHeader *)pbData)-l)
#endif // _CRTDBG_INTERNALS_H
_CrtMemBiockHeader, _crtMemstate, _crtMemCheckpoint, _crtMemBiockHeader. , DCRT- .
- MEMDUMPERVALIDATOR.CPP, BUGSLAYERUTIL.DLL, , API- HeapCreate, . , hook-, DCRT-, , . , . malloc, , hook- .
MemDumperValidator , , , . , , , MemDumperValidator, : , .
, .
MemDumperValidator : , _CrtsetDbgFiag , - .
, , , MemDumperValidator: DCRT-- -, , . - DVINFO. MemDumperValidator , , - .
, #pragma init_seg, . #pragma init_seg , , no-, : compiler, lib, user, section name funcname. .
compiler Microsoft. , compiler, , . , lib, compiler, , user, , .
, C++ . , , . . , .
MemDumperValidator , , lib #pragma init_seg . lib- ( ) , . : #pragma init_seg (compiler). , , compiler .
C++, MemDumperValidator ( AutoMatic), _CrtSetDbgFiag. , DCRT- . , DCRT-, . MemDumperValidator -, C++ , .
, , , MemDumperValidator. , , , . DCRT--. "" , DCRT- _crtsetDumpclient NULL ( ), , , _crtDumpMemoryLeaks. , . , , .
DCRT-, BoundsChecker?
, BoundsChecker Compuware NuMega Purify Rational Software , (underwrites overwrites) . , , , , DCRT- . , , , , - (debug build) . , , 2, .
DCRT- . , , , , . . DCRT- , . , .
#pragma init_seg(compiler) AutoMatic ( ), , _CRTDBG_LEAK_CHECK_DF, DCRT- . , CRT- BUGSLAYERUTIL.LIB ( /NODEFAULTLIB). BUGSLAYERUTIL.LIB , CRT- #pragma init_seg(compiler), , , , .
. .
, DCRT- . - CRT- (, printf), , , _crtDumpMemoryLeaks, CRT- - . DCRT- , . . MemDumperValidator , DCRT-. , - _RPTn _RPTFH, _CrtDumpMemoryLeaks.
. , . , 32- Windows , . 16- Windows ( 16- Windows 98) STRESS.EXE, SDK . , , "" (heap) (Graphics Device Interface GDI), . , .
"" 32- Windows, DCRT- ( ) . MemStress /++-. ( , "" .) MemStress, ( Visual Basic), .
MemStress (), : , n- , , n , . , MemStress , , . DCRT-, . MFC- MemStressDemo ( -) (UI) MemStress .
MemStress . BUGSLAYERUTIL.H MEMSTRESSINIT . , MEMSTRESSTERMINATE. .
, MemStress UI, Add Program ( ) , MEMSTRESSINIT. Save Settings For This Program, MEMSTRESS.INI. .
MemStress . , , , 100 , , initinstance MFC- MEMSTRESSINIT, MFC . , MemStress .
MemStress MEMSTRESS.INI, . DCRT-, _CrtSetAilocHook MemStress, AllocationHook. TRUE, , FALSE , DCRT- . DCRT- : nBiockUse _CRT_BLOCK, hook- TRUE, .
. , : _HOOK_ALLOC, _HOOK_REALLOC _HOOK_FREE). AllocationHook _HOOK_FREE, , , . _HOOK_ALLOC _HOOK_REALLOC AiiocationHook if-, , - . , FALSE.
, , MemStress . , MemStressDemo ( MFC), . MemStress ( ) , , , MemStressDemo . ( , ), , , .
, , . , , . , API- MessageBox - ( ). , - MessageBox. AiiocationHook, MessageBox. , , .
, API- MessageBox - MFC. _AfxActivationWndProc , CWND::FromHandie, , MFC () cobject. ? , _AfxActivationWndProc "" () . MFC -, . ( message box), MFC .
, , . , , . . . , , AiiocationHook, , , AiiocationHook.
, , AiiocationHook. 0, AiiocationHook MessageBox, . , , .
Computer-Based Training, . .
"quick dynamic thread local storage solution" . .
- (DCRT-) , . DCRT- MemDumperValidator MemStress. , , .
DCRT-. , , .
![]() | 10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |
|
![]() |