home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dumpinit.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  4KB  |  157 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef _DEBUG   // entire file
  14.  
  15. #ifdef AFX_AUX_SEG
  16. #pragma code_seg(AFX_AUX_SEG)
  17. #endif
  18.  
  19. // You must have AFX.INI (from \MSVC20\MFC\SRC) in your Windows
  20. // directory if you desire diagnostic output.
  21.  
  22. // See Technical note TN007 for a description of
  23. //   afxTraceFlags and afxTraceEnabled.
  24.  
  25. AFX_DATADEF CDumpContext afxDump;
  26. AFX_DATADEF BOOL afxTraceEnabled;
  27. AFX_DATADEF UINT afxTraceFlags;
  28. BOOL _afxDiagnosticInit = AfxDiagnosticInit();
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // _AFX_DEBUG_STATE implementation
  32.  
  33. static const TCHAR szIniFile[] = _T("AFX.INI");
  34. static const TCHAR szDiagSection[] = _T("Diagnostics");
  35. static const TCHAR szTraceEnabled[] = _T("TraceEnabled");
  36. static const TCHAR szTraceFlags[] = _T("TraceFlags");
  37.  
  38. #ifndef _AFX_NO_DEBUG_CRT
  39. static _CRT_DUMP_CLIENT pfnOldCrtDumpClient = NULL;
  40. static _CRT_REPORT_HOOK pfnOldCrtReportHook = NULL;
  41.  
  42. void __cdecl _AfxCrtDumpClient(void * pvData, unsigned int nBytes)
  43. {
  44.     char sz[256];
  45.     CObject* pObject = (CObject*)pvData;
  46.  
  47. #ifndef _AFX_PORTABLE
  48.     // use SEH (structured exception handling) to catch even GPFs
  49.     //  that result from partially valid objects.
  50.     __try
  51. #endif
  52.     {
  53.         // with vtable, verify that object and vtable are valid
  54.         if (!AfxIsValidAddress(*(void**)pObject, sizeof(void*), FALSE) ||
  55.             !AfxIsValidAddress(pObject, pObject->GetRuntimeClass()->m_nObjectSize, FALSE))
  56.         {
  57.             // short form for invalid objects
  58.             wsprintfA(sz, "an invalid object at $%08lX, %u bytes long\n",
  59.                 pvData, nBytes);
  60.             afxDump << sz;
  61.         }
  62.         else if (afxDump.GetDepth() > 0)
  63.         {
  64.             // long form
  65.             pObject->Dump(afxDump);
  66.             afxDump << "\n";
  67.         }
  68.         else
  69.         {
  70.             // short form
  71.             wsprintfA(sz, "a %hs object at $%08lX, %u bytes long\n",
  72.                 pObject->GetRuntimeClass()->m_lpszClassName, pvData, nBytes);
  73.             afxDump << sz;
  74.         }
  75.     }
  76. #ifndef _AFX_PORTABLE
  77.     __except(EXCEPTION_EXECUTE_HANDLER)
  78.     {
  79.         // short form for trashed objects
  80.         wsprintfA(sz, "faulted while dumping object at $%08lX, %u bytes long\n",
  81.             pvData, nBytes);
  82.         afxDump << sz;
  83.     }
  84. #endif
  85.  
  86.     if (pfnOldCrtDumpClient != NULL)
  87.         (*pfnOldCrtDumpClient)(pvData, nBytes);
  88.     return;
  89. }
  90.  
  91. int __cdecl _AfxCrtReportHook(int nRptType, char *szMsg, int* pResult)
  92. {
  93.     // call the old report hook if there was one
  94.     if (pfnOldCrtReportHook != NULL &&
  95.         (*pfnOldCrtReportHook)(nRptType, szMsg, pResult))
  96.     {
  97.         return TRUE;
  98.     }
  99.  
  100.     // no hook on asserts or when m_pFile is NULL
  101.     if (nRptType == _CRT_ASSERT || afxDump.m_pFile == NULL)
  102.         return FALSE;
  103.  
  104.     // non-NULL m_pFile, so go through afxDump for the message
  105.     *pResult = FALSE;
  106.     afxDump << szMsg;
  107.     return TRUE;
  108. }
  109. #endif // _AFX_NO_DEBUG_CRT
  110.  
  111. _AFX_DEBUG_STATE::_AFX_DEBUG_STATE()
  112. {
  113.     afxTraceEnabled = ::GetPrivateProfileInt(szDiagSection, szTraceEnabled,
  114.         TRUE, szIniFile);
  115.     afxTraceFlags = ::GetPrivateProfileInt(szDiagSection, szTraceFlags,
  116.         0, szIniFile);
  117.  
  118. #ifndef _AFX_NO_DEBUG_CRT
  119.     ASSERT(pfnOldCrtDumpClient == NULL);
  120.     pfnOldCrtDumpClient = _CrtSetDumpClient(_AfxCrtDumpClient);
  121.  
  122.     ASSERT(pfnOldCrtReportHook == NULL);
  123.     pfnOldCrtReportHook = _CrtSetReportHook(_AfxCrtReportHook);
  124.     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);
  125. #endif // _AFX_NO_DEBUG_CRT
  126. }
  127.  
  128. _AFX_DEBUG_STATE::~_AFX_DEBUG_STATE()
  129. {
  130. #ifndef _AFX_NO_DEBUG_CRT
  131.     _CrtDumpMemoryLeaks();
  132.     int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  133.     _CrtSetDbgFlag(nOldState & ~_CRTDBG_LEAK_CHECK_DF);
  134.  
  135.     _CrtSetReportHook(pfnOldCrtReportHook);
  136.     _CrtSetDumpClient(pfnOldCrtDumpClient);
  137. #endif // _AFX_NO_DEBUG_CRT
  138. }
  139.  
  140. #pragma warning(disable: 4074)
  141. #pragma init_seg(lib)
  142.  
  143. PROCESS_LOCAL(_AFX_DEBUG_STATE, afxDebugState)
  144.  
  145. BOOL AFXAPI AfxDiagnosticInit(void)
  146. {
  147.     // just get the debug state to cause initialization
  148.     _AFX_DEBUG_STATE* pState = afxDebugState.GetData();
  149.     ASSERT(pState != NULL);
  150.  
  151.     return TRUE;
  152. }
  153.  
  154. #endif //_DEBUG
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157.