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
This commit is contained in:
Václav Slavík
2004-02-28 23:57:54 +00:00
parent 121680bff0
commit ced5554416
3 changed files with 102 additions and 32 deletions

View File

@@ -18,6 +18,7 @@
#include "wx/defs.h" #include "wx/defs.h"
#include "wx/string.h" #include "wx/string.h"
#include "wx/msgout.h"
/* /*
The macro which will be expanded to include the file and line number The macro which will be expanded to include the file and line number
@@ -288,6 +289,9 @@ public:
// that are outstanding // that are outstanding
static int CountObjectsLeft(bool sinceCheckpoint = FALSE); static int CountObjectsLeft(bool sinceCheckpoint = FALSE);
// This function is used to output the dump
static void OutputDumpLine(const wxChar *szFormat, ...);
private: private:
// Store these here to allow access to the list without // Store these here to allow access to the list without
// needing to have a wxMemStruct object. // needing to have a wxMemStruct object.
@@ -299,6 +303,31 @@ private:
static bool m_checkPrevious; 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. // Output a debug message, in a system dependent fashion.
void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1; void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1;
void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_PRINTF_2; 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 WXTRACE wxTrace
#define WXTRACELEVEL wxTraceLevel #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 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
// 819700) // 819700)
@@ -321,7 +352,7 @@ void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_P
#define WXTRACE TRUE ? (void)0 : wxTrace #define WXTRACE TRUE ? (void)0 : wxTrace
#define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
#endif // __WXDEBUG__ #endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
#endif #endif
// _WX_MEMORYH__ // _WX_MEMORYH__

View File

@@ -338,16 +338,7 @@ static void DoCommonPostCleanup()
FreeConvertedArgs(); FreeConvertedArgs();
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE
// check for memory leaks // Note: check for memory leaks is now done via wxDebugContextDumpDelayCounter
#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
#if wxUSE_LOG #if wxUSE_LOG
// and now delete the last logger as well // and now delete the last logger as well
delete wxLog::SetActiveTarget(NULL); delete wxLog::SetActiveTarget(NULL);

View File

@@ -343,7 +343,7 @@ void wxMemStruct::PrintNode ()
msg += wxT("object"); msg += wxT("object");
wxString msg2; 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; msg += msg2;
wxLogMessage(msg); wxLogMessage(msg);
@@ -356,7 +356,7 @@ void wxMemStruct::PrintNode ()
msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum); msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum);
msg += wxT("non-object data"); msg += wxT("non-object data");
wxString msg2; 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; msg += msg2;
wxLogMessage(msg); wxLogMessage(msg);
@@ -389,10 +389,10 @@ void wxMemStruct::Dump ()
msg += wxT("unknown object class"); msg += wxT("unknown object class");
wxString msg2(wxT("")); 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; msg += msg2;
wxLogMessage(msg); wxDebugContext::OutputDumpLine(msg);
} }
else else
{ {
@@ -401,9 +401,9 @@ void wxMemStruct::Dump ()
msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum); msg.Printf(wxT("%s(%d): "), m_fileName, (int)m_lineNum);
wxString msg2(wxT("")); 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; msg += msg2;
wxLogMessage(msg); wxDebugContext::OutputDumpLine(msg);
} }
} }
@@ -613,18 +613,18 @@ bool wxDebugContext::Dump(void)
{ {
appNameStr = wxTheApp->GetAppName(); appNameStr = wxTheApp->GetAppName();
appName = WXSTRINGCAST appNameStr; 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 else
{ {
wxLogMessage( wxT("----- Memory dump -----") ); OutputDumpLine( wxT("----- Memory dump -----") );
} }
} }
TraverseList ((PmSFV)&wxMemStruct::Dump, (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL)); TraverseList ((PmSFV)&wxMemStruct::Dump, (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL));
wxLogMessage( wxT("") ); OutputDumpLine( wxT("") );
wxLogMessage( wxT("") ); OutputDumpLine( wxT("") );
return TRUE; return TRUE;
#else #else
@@ -669,11 +669,11 @@ bool wxDebugContext::PrintStatistics(bool detailed)
{ {
appNameStr = wxTheApp->GetAppName(); appNameStr = wxTheApp->GetAppName();
appName = WXSTRINGCAST appNameStr; 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 else
{ {
wxLogMessage( wxT("----- Memory statistics -----") ); OutputDumpLine( wxT("----- Memory statistics -----") );
} }
} }
@@ -730,22 +730,22 @@ bool wxDebugContext::PrintStatistics(bool detailed)
{ {
while (list) 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); list->instanceCount, list->instanceClass, list->totalSize);
wxDebugStatsStruct *old = list; wxDebugStatsStruct *old = list;
list = old->next; list = old->next;
free((char *)old); free((char *)old);
} }
wxLogMessage(wxT("")); OutputDumpLine(wxT(""));
} }
SetDebugMode(currentMode); SetDebugMode(currentMode);
wxLogMessage(wxT("Number of object items: %ld"), noObjectNodes); OutputDumpLine(wxT("Number of object items: %ld"), noObjectNodes);
wxLogMessage(wxT("Number of non-object items: %ld"), noNonObjectNodes); OutputDumpLine(wxT("Number of non-object items: %ld"), noNonObjectNodes);
wxLogMessage(wxT("Total allocated size: %ld"), totalSize); OutputDumpLine(wxT("Total allocated size: %ld"), totalSize);
wxLogMessage(wxT("")); OutputDumpLine(wxT(""));
wxLogMessage(wxT("")); OutputDumpLine(wxT(""));
return TRUE; return TRUE;
#else #else
@@ -858,6 +858,31 @@ int wxDebugContext::CountObjectsLeft(bool sinceCheckpoint)
return n ; 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 #if USE_THREADSAFE_MEMORY_ALLOCATION
static bool memSectionOk = FALSE; static bool memSectionOk = FALSE;
@@ -1075,5 +1100,28 @@ void wxTraceLevel(int, const wxChar * ...)
#endif #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