From ced55544160feffbf02445e73ce4693285940c0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sat, 28 Feb 2004 23:57:54 +0000 Subject: [PATCH] fixed wxDebugContext to work with global and static objects (patch 901031) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25998 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/memory.h | 35 ++++++++++++++++- src/common/init.cpp | 11 +----- src/common/memory.cpp | 88 +++++++++++++++++++++++++++++++++---------- 3 files changed, 102 insertions(+), 32 deletions(-) diff --git a/include/wx/memory.h b/include/wx/memory.h index 8ae2be6e81..3b58cb55d9 100644 --- a/include/wx/memory.h +++ b/include/wx/memory.h @@ -18,6 +18,7 @@ #include "wx/defs.h" #include "wx/string.h" +#include "wx/msgout.h" /* The macro which will be expanded to include the file and line number @@ -288,6 +289,9 @@ public: // that are outstanding static int CountObjectsLeft(bool sinceCheckpoint = FALSE); + // This function is used to output the dump + static void OutputDumpLine(const wxChar *szFormat, ...); + private: // Store these here to allow access to the list without // needing to have a wxMemStruct object. @@ -299,6 +303,31 @@ private: static bool m_checkPrevious; }; +// Final cleanup (e.g. deleting the log object and doing memory leak checking) +// will be delayed until all wxDebugContextDumpDelayCounter objects have been +// destructed. Adding one wxDebugContextDumpDelayCounter per file will delay +// memory leak checking until after destructing all global objects. +class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter +{ +public: + wxDebugContextDumpDelayCounter() { + sm_count++; + } + + ~wxDebugContextDumpDelayCounter() { + sm_count--; + if(!sm_count) DoDump(); + } +private: + void DoDump(); + static int sm_count; +}; + +// make leak dump after all globals have been destructed +static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File; +#define WXDEBUG_DUMPDELAYCOUNTER \ + static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra; + // Output a debug message, in a system dependent fashion. void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1; void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_PRINTF_2; @@ -306,7 +335,9 @@ void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_P #define WXTRACE wxTrace #define WXTRACELEVEL wxTraceLevel -#else // else part for the #if __WXDEBUG__ +#else // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT + +#define WXDEBUG_DUMPDELAYCOUNTER // Borland C++ Builder 6 seems to have troubles with inline functions (see bug // 819700) @@ -321,7 +352,7 @@ void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_P #define WXTRACE TRUE ? (void)0 : wxTrace #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel -#endif // __WXDEBUG__ +#endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT #endif // _WX_MEMORYH__ diff --git a/src/common/init.cpp b/src/common/init.cpp index a67f723b64..e6d803cbe4 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -338,16 +338,7 @@ static void DoCommonPostCleanup() FreeConvertedArgs(); #endif // wxUSE_UNICODE - // check for memory leaks -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - if (wxDebugContext::CountObjectsLeft(TRUE) > 0) - { - wxLogDebug(wxT("There were memory leaks.\n")); - wxDebugContext::Dump(); - wxDebugContext::PrintStatistics(); - } -#endif // Debug - + // Note: check for memory leaks is now done via wxDebugContextDumpDelayCounter #if wxUSE_LOG // and now delete the last logger as well delete wxLog::SetActiveTarget(NULL); diff --git a/src/common/memory.cpp b/src/common/memory.cpp index e762055fc5..c980106cbb 100644 --- a/src/common/memory.cpp +++ b/src/common/memory.cpp @@ -343,7 +343,7 @@ void wxMemStruct::PrintNode () msg += wxT("object"); wxString msg2; - msg2.Printf(wxT(" at $%lX, size %d"), (long)GetActualData(), (int)RequestSize()); + msg2.Printf(wxT(" at 0x%lX, size %d"), (long)GetActualData(), (int)RequestSize()); msg += msg2; wxLogMessage(msg); @@ -356,7 +356,7 @@ void wxMemStruct::PrintNode () msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum); msg += wxT("non-object data"); wxString msg2; - msg2.Printf(wxT(" at $%lX, size %d\n"), (long)GetActualData(), (int)RequestSize()); + msg2.Printf(wxT(" at 0x%lX, size %d\n"), (long)GetActualData(), (int)RequestSize()); msg += msg2; wxLogMessage(msg); @@ -389,10 +389,10 @@ void wxMemStruct::Dump () msg += wxT("unknown object class"); wxString msg2(wxT("")); - msg2.Printf(wxT(" at $%lX, size %d"), (long)GetActualData(), (int)RequestSize()); + msg2.Printf(wxT(" at 0x%lX, size %d"), (long)GetActualData(), (int)RequestSize()); msg += msg2; - wxLogMessage(msg); + wxDebugContext::OutputDumpLine(msg); } else { @@ -401,9 +401,9 @@ void wxMemStruct::Dump () msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum); wxString msg2(wxT("")); - msg2.Printf(wxT("non-object data at $%lX, size %d"), (long)GetActualData(), (int)RequestSize() ); + msg2.Printf(wxT("non-object data at 0x%lX, size %d"), (long)GetActualData(), (int)RequestSize() ); msg += msg2; - wxLogMessage(msg); + wxDebugContext::OutputDumpLine(msg); } } @@ -613,18 +613,18 @@ bool wxDebugContext::Dump(void) { appNameStr = wxTheApp->GetAppName(); appName = WXSTRINGCAST appNameStr; - wxLogMessage(wxT("----- Memory dump of %s at %s -----"), appName, WXSTRINGCAST wxNow() ); + OutputDumpLine(wxT("----- Memory dump of %s at %s -----"), appName, WXSTRINGCAST wxNow() ); } else { - wxLogMessage( wxT("----- Memory dump -----") ); + OutputDumpLine( wxT("----- Memory dump -----") ); } } TraverseList ((PmSFV)&wxMemStruct::Dump, (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL)); - wxLogMessage( wxT("") ); - wxLogMessage( wxT("") ); + OutputDumpLine( wxT("") ); + OutputDumpLine( wxT("") ); return TRUE; #else @@ -669,11 +669,11 @@ bool wxDebugContext::PrintStatistics(bool detailed) { appNameStr = wxTheApp->GetAppName(); appName = WXSTRINGCAST appNameStr; - wxLogMessage(wxT("----- Memory statistics of %s at %s -----"), appName, WXSTRINGCAST wxNow() ); + OutputDumpLine(wxT("----- Memory statistics of %s at %s -----"), appName, WXSTRINGCAST wxNow() ); } else { - wxLogMessage( wxT("----- Memory statistics -----") ); + OutputDumpLine( wxT("----- Memory statistics -----") ); } } @@ -730,22 +730,22 @@ bool wxDebugContext::PrintStatistics(bool detailed) { while (list) { - wxLogMessage(wxT("%ld objects of class %s, total size %ld"), + OutputDumpLine(wxT("%ld objects of class %s, total size %ld"), list->instanceCount, list->instanceClass, list->totalSize); wxDebugStatsStruct *old = list; list = old->next; free((char *)old); } - wxLogMessage(wxT("")); + OutputDumpLine(wxT("")); } SetDebugMode(currentMode); - wxLogMessage(wxT("Number of object items: %ld"), noObjectNodes); - wxLogMessage(wxT("Number of non-object items: %ld"), noNonObjectNodes); - wxLogMessage(wxT("Total allocated size: %ld"), totalSize); - wxLogMessage(wxT("")); - wxLogMessage(wxT("")); + OutputDumpLine(wxT("Number of object items: %ld"), noObjectNodes); + OutputDumpLine(wxT("Number of non-object items: %ld"), noNonObjectNodes); + OutputDumpLine(wxT("Total allocated size: %ld"), totalSize); + OutputDumpLine(wxT("")); + OutputDumpLine(wxT("")); return TRUE; #else @@ -858,6 +858,31 @@ int wxDebugContext::CountObjectsLeft(bool sinceCheckpoint) return n ; } +// This function is used to output the dump +void wxDebugContext::OutputDumpLine(const wxChar *szFormat, ...) +{ + // a buffer of 2048 bytes should be long enough for a file name + // and a class name + wxChar buf[2048]; + int count; + va_list argptr; + va_start(argptr, szFormat); + buf[sizeof(buf)-1] = _T('\0'); + + // keep 3 bytes for a \r\n\0 + count = wxVsnprintf(buf, sizeof(buf)-3, szFormat, argptr); + + if ( count < 0 ) + count = sizeof(buf)-3; + buf[count]=_T('\r'); + buf[count+1]=_T('\n'); + buf[count+2]=_T('\0'); + + wxMessageOutputDebug dbgout; + dbgout.Printf(buf); +} + + #if USE_THREADSAFE_MEMORY_ALLOCATION static bool memSectionOk = FALSE; @@ -1075,5 +1100,28 @@ void wxTraceLevel(int, const wxChar * ...) #endif } -#endif +//---------------------------------------------------------------------------- +// Final cleanup after all global objects in all files have been destructed +//---------------------------------------------------------------------------- + +// Don't set it to 0 by dynamic initialization +// Some compilers will realy do the asignment later +// All global variables are initialized to 0 at the very beginning, and this is just fine. +int wxDebugContextDumpDelayCounter::sm_count; + +void wxDebugContextDumpDelayCounter::DoDump() +{ + if (wxDebugContext::CountObjectsLeft(TRUE) > 0) + { + wxDebugContext::OutputDumpLine(wxT("There were memory leaks.\n")); + wxDebugContext::Dump(); + wxDebugContext::PrintStatistics(); + } +} + +// Even if there is nothing else, make sure that there is at +// least one clenup counter object +static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_One; + +#endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT -- 2.45.2