home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DUMPINIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  4.5 KB  |  161 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. #ifndef _MAC
  34. static const TCHAR szIniFile[] = _T("AFX.INI");
  35. #else
  36. static const TCHAR szIniFile[] = _T("AFX Preferences");
  37. #endif
  38. static const TCHAR szDiagSection[] = _T("Diagnostics");
  39. static const TCHAR szTraceEnabled[] = _T("TraceEnabled");
  40. static const TCHAR szTraceFlags[] = _T("TraceFlags");
  41.  
  42. #ifndef _AFX_NO_DEBUG_CRT
  43. static _CRT_DUMP_CLIENT pfnOldCrtDumpClient = NULL;
  44. static _CRT_REPORT_HOOK pfnOldCrtReportHook = NULL;
  45.  
  46. void __cdecl _AfxCrtDumpClient(void * pvData, unsigned int nBytes)
  47. {
  48.     char sz[256];
  49.     CObject* pObject = (CObject*)pvData;
  50.  
  51. #if !defined(_MAC) && !defined(_AFX_PORTABLE)
  52.     // use SEH (structured exception handling) to catch even GPFs
  53.     //  that result from partially valid objects.
  54.     __try
  55. #endif
  56.     {
  57.         // with vtable, verify that object and vtable are valid
  58.         if (!AfxIsValidAddress(*(void**)pObject, sizeof(void*), FALSE) ||
  59.             !AfxIsValidAddress(pObject, pObject->GetRuntimeClass()->m_nObjectSize, FALSE))
  60.         {
  61.             // short form for invalid objects
  62.             wsprintfA(sz, "an invalid object at $%08lX, %u bytes long\n",
  63.                 pvData, nBytes);
  64.             afxDump << sz;
  65.         }
  66.         else if (afxDump.GetDepth() > 0)
  67.         {
  68.             // long form
  69.             pObject->Dump(afxDump);
  70.             afxDump << "\n";
  71.         }
  72.         else
  73.         {
  74.             // short form
  75.             wsprintfA(sz, "a %hs object at $%08lX, %u bytes long\n",
  76.                 pObject->GetRuntimeClass()->m_lpszClassName, pvData, nBytes);
  77.             afxDump << sz;
  78.         }
  79.     }
  80. #if !defined(_MAC) && !defined(_AFX_PORTABLE)
  81.     __except(EXCEPTION_EXECUTE_HANDLER)
  82.     {
  83.         // short form for trashed objects
  84.         wsprintfA(sz, "faulted while dumping object at $%08lX, %u bytes long\n",
  85.             pvData, nBytes);
  86.         afxDump << sz;
  87.     }
  88. #endif
  89.  
  90.     if (pfnOldCrtDumpClient != NULL)
  91.         (*pfnOldCrtDumpClient)(pvData, nBytes);
  92.     return;
  93. }
  94.  
  95. int __cdecl _AfxCrtReportHook(int nRptType, char *szMsg, int* pResult)
  96. {
  97.     // call the old report hook if there was one
  98.     if (pfnOldCrtReportHook != NULL &&
  99.         (*pfnOldCrtReportHook)(nRptType, szMsg, pResult))
  100.     {
  101.         return TRUE;
  102.     }
  103.  
  104.     // no hook on asserts or when m_pFile is NULL
  105.     if (nRptType == _CRT_ASSERT || afxDump.m_pFile == NULL)
  106.         return FALSE;
  107.  
  108.     // non-NULL m_pFile, so go through afxDump for the message
  109.     *pResult = FALSE;
  110.     afxDump << szMsg;
  111.     return TRUE;
  112. }
  113. #endif // _AFX_NO_DEBUG_CRT
  114.  
  115. _AFX_DEBUG_STATE::_AFX_DEBUG_STATE()
  116. {
  117.     afxTraceEnabled = ::GetPrivateProfileInt(szDiagSection, szTraceEnabled,
  118.         TRUE, szIniFile);
  119.     afxTraceFlags = ::GetPrivateProfileInt(szDiagSection, szTraceFlags,
  120.         0, szIniFile);
  121.  
  122. #ifndef _AFX_NO_DEBUG_CRT
  123.     ASSERT(pfnOldCrtDumpClient == NULL);
  124.     pfnOldCrtDumpClient = _CrtSetDumpClient(_AfxCrtDumpClient);
  125.  
  126.     ASSERT(pfnOldCrtReportHook == NULL);
  127.     pfnOldCrtReportHook = _CrtSetReportHook(_AfxCrtReportHook);
  128.     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);
  129. #endif // _AFX_NO_DEBUG_CRT
  130. }
  131.  
  132. _AFX_DEBUG_STATE::~_AFX_DEBUG_STATE()
  133. {
  134. #ifndef _AFX_NO_DEBUG_CRT
  135.     _CrtDumpMemoryLeaks();
  136.     int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  137.     _CrtSetDbgFlag(nOldState & ~_CRTDBG_LEAK_CHECK_DF);
  138.  
  139.     _CrtSetReportHook(pfnOldCrtReportHook);
  140.     _CrtSetDumpClient(pfnOldCrtDumpClient);
  141. #endif // _AFX_NO_DEBUG_CRT
  142. }
  143.  
  144. #pragma warning(disable: 4074)
  145. #pragma init_seg(lib)
  146.  
  147. PROCESS_LOCAL(_AFX_DEBUG_STATE, afxDebugState)
  148.  
  149. BOOL AFXAPI AfxDiagnosticInit(void)
  150. {
  151.     // just get the debug state to cause initialization
  152.     _AFX_DEBUG_STATE* pState = afxDebugState.GetData();
  153.     ASSERT(pState != NULL);
  154.  
  155.     return TRUE;
  156. }
  157.  
  158. #endif //_DEBUG
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161.