X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b2aef89b7a675805b601e757492c982031f38d21..884470b12ee25b20796bfef11a9bfa5fb1e1511c:/src/common/memory.cpp diff --git a/src/common/memory.cpp b/src/common/memory.cpp index 819dea4bcb..81245b504f 100644 --- a/src/common/memory.cpp +++ b/src/common/memory.cpp @@ -24,7 +24,7 @@ #include "wx/defs.h" #endif -#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT +#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT #ifdef __GNUG__ // #pragma implementation @@ -35,23 +35,28 @@ #include "wx/app.h" #endif +#include #include -#if USE_IOSTREAMH +#if wxUSE_IOSTREAMH #include +#include #else #include +#include +# ifdef _MSC_VER + using namespace std; +# endif #endif -#include -#if !defined(__WATCOMC__) && !defined(__VMS__) +#if !defined(__WATCOMC__) && !defined(__VMS__) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) #include #endif #include #include -#ifdef __WINDOWS__ +#ifdef __WXMSW__ #include #ifdef GetClassInfo @@ -66,11 +71,9 @@ #include "wx/memory.h" -/* #ifdef new #undef new #endif -*/ // wxDebugContext wxTheDebugContext; /* @@ -110,7 +113,7 @@ */ void wxMemStruct::ErrorMsg (const char * mesg) { - wxTrace("wxWindows memory checking error: %s\n", mesg); + wxLogDebug("wxWindows memory checking error: %s", mesg); PrintNode (); // << m_fileName << ' ' << m_lineNum << endl; @@ -121,7 +124,7 @@ void wxMemStruct::ErrorMsg (const char * mesg) */ void wxMemStruct::ErrorMsg () { - wxTrace("wxWindows over/underwrite memory error: \n"); + wxLogDebug("wxWindows over/underwrite memory error:"); PrintNode (); // cerr << m_fileName << ' ' << m_lineNum << endl; @@ -327,22 +330,36 @@ void wxMemStruct::PrintNode () wxObject *obj = (wxObject *)m_actualData; wxClassInfo *info = obj->GetClassInfo(); + // Let's put this in standard form so IDEs can load the file at the appropriate + // line + wxString msg(""); + + if (m_fileName) + msg.Printf("%s(%d): ", m_fileName, (int)m_lineNum); + if (info && info->GetClassName()) - wxTrace("%s", info->GetClassName()); + msg += info->GetClassName(); else - wxTrace("Object"); + msg += "object"; - if (m_fileName) - wxTrace(" (%s %d)", m_fileName, (int)m_lineNum); + wxString msg2; + msg2.Printf(" at $%lX, size %d", (long)GetActualData(), (int)RequestSize()); + msg += msg2; - wxTrace(" at $%lX, size %d\n", (long)GetActualData(), (int)RequestSize()); + wxLogDebug(msg); } else { - wxTrace("Non-object data"); + wxString msg(""); + if (m_fileName) - wxTrace(" (%s %d)", m_fileName, (int)m_lineNum); - wxTrace(" at $%lX, size %d\n", (long)GetActualData(), (int)RequestSize()); + msg.Printf("%s(%d): ", m_fileName, (int)m_lineNum); + msg += ("non-object data"); + wxString msg2; + msg2.Printf(" at $%lX, size %d\n", (long)GetActualData(), (int)RequestSize()); + msg += msg2; + + wxLogDebug(msg); } } @@ -353,24 +370,40 @@ void wxMemStruct::Dump () if (m_isObject) { wxObject *obj = (wxObject *)m_actualData; -// wxClassInfo *info = obj->GetClassInfo(); + wxString msg(""); if (m_fileName) - wxTrace("Item (%s %d)", m_fileName, (int)m_lineNum); - else - wxTrace("Item"); + msg.Printf("%s(%d): ", m_fileName, (int)m_lineNum); + - wxTrace(" at $%lX, size %d: ", (long)GetActualData(), (int)RequestSize()); -// wxTrace(info->GetClassName()); + /* TODO: We no longer have a stream (using wxLogDebug) so we can't dump it. + * Instead, do what wxObject::Dump does. + * What should we do long-term, eliminate Dumping? Or specify + * that MyClass::Dump should use wxLogDebug? Ugh. obj->Dump(wxDebugContext::GetStream()); - wxTrace("\n"); + */ + + if (obj->GetClassInfo() && obj->GetClassInfo()->GetClassName()) + msg += obj->GetClassInfo()->GetClassName(); + else + msg += "unknown object class"; + + wxString msg2(""); + msg2.Printf(" at $%lX, size %d", (long)GetActualData(), (int)RequestSize()); + msg += msg2; + + wxLogDebug(msg); } else { - wxTrace("Non-object data"); + wxString msg(""); if (m_fileName) - wxTrace(" (%s %d)", m_fileName, (int)m_lineNum); - wxTrace(" at $%lX, size %d\n", (long)GetActualData(), (int)RequestSize()); + msg.Printf("%s(%d): ", m_fileName, (int)m_lineNum); + + wxString msg2(""); + msg2.Printf("non-object data at $%lX, size %d", (long)GetActualData(), (int)RequestSize() ); + msg += msg2; + wxLogDebug(msg); } } @@ -387,7 +420,9 @@ int wxMemStruct::ValidateNode () ErrorMsg ("Object already deleted"); else { // Can't use the error routines as we have no recognisable object. - wxTrace("Can't verify memory struct - all bets are off!\n"); +#ifndef __WXGTK__ + wxLogDebug("Can't verify memory struct - all bets are off!"); +#endif } return 0; } @@ -458,15 +493,6 @@ wxDebugContext::~wxDebugContext(void) void wxDebugContext::SetStream(ostream *str, streambuf *buf) { -/* - if (str) - { - char buff[128]; - sprintf(buff, "SetStream (1): str is %ld", (long) str); - MessageBox(NULL, buff, "Memory", MB_OK); - } -*/ - if (m_debugStream) { m_debugStream->flush(); @@ -499,11 +525,6 @@ bool wxDebugContext::SetFile(const wxString& file) } else { -/* - char buf[40]; - sprintf(buf, "SetFile: str is %ld", (long) str); - MessageBox(NULL, buf, "Memory", MB_OK); -*/ SetStream(str); return TRUE; } @@ -511,6 +532,8 @@ bool wxDebugContext::SetFile(const wxString& file) bool wxDebugContext::SetStandardError(void) { + // Obsolete +#if 0 #if !defined(_WINDLL) wxDebugStreamBuf *buf = new wxDebugStreamBuf; ostream *stream = new ostream(m_streamBuf); @@ -519,6 +542,8 @@ bool wxDebugContext::SetStandardError(void) #else return FALSE; #endif +#endif + return FALSE; } @@ -610,7 +635,8 @@ void wxDebugContext::TraverseList (PmSFV func, wxMemStruct *from) for (wxMemStruct * st = from; st != 0; st = st->m_next) { void* data = st->GetActualData(); - if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) +// if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) + if (data != (void*) wxLog::GetActiveTarget()) { (st->*func) (); } @@ -623,11 +649,11 @@ void wxDebugContext::TraverseList (PmSFV func, wxMemStruct *from) */ bool wxDebugContext::PrintList (void) { -#if WXDEBUG - if (!HasStream()) - return FALSE; +#ifdef __WXDEBUG__ +// if (!HasStream()) +// return FALSE; - TraverseList ((PmSFV)&wxMemStruct::PrintNode, (checkPoint ? checkPoint->m_next : NULL)); + TraverseList ((PmSFV)&wxMemStruct::PrintNode, (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL)); return TRUE; #else @@ -637,9 +663,9 @@ bool wxDebugContext::PrintList (void) bool wxDebugContext::Dump(void) { -#if WXDEBUG - if (!HasStream()) - return FALSE; +#ifdef __WXDEBUG__ +// if (!HasStream()) +// return FALSE; if (TRUE) { @@ -649,10 +675,17 @@ bool wxDebugContext::Dump(void) { appNameStr = wxTheApp->GetAppName(); appName = (char*) (const char*) appNameStr; - wxTrace("Memory dump of %s at %s:\n", appName, wxNow()); + wxLogDebug("----- Memory dump of %s at %s -----", appName, WXSTRINGCAST wxNow() ); + } + else + { + wxLogDebug( "----- Memory dump -----" ); } } - TraverseList ((PmSFV)&wxMemStruct::Dump, (checkPoint ? checkPoint->m_next : NULL)); + TraverseList ((PmSFV)&wxMemStruct::Dump, (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL)); + + wxLogDebug( "" ); + wxLogDebug( "" ); return TRUE; #else @@ -687,10 +720,26 @@ static wxDebugStatsStruct *InsertStatsStruct(wxDebugStatsStruct *head, wxDebugSt bool wxDebugContext::PrintStatistics(bool detailed) { -#if WXDEBUG - if (!HasStream()) - return FALSE; +#ifdef __WXDEBUG__ +// if (!HasStream()) +// return FALSE; + if (TRUE) + { + char* appName = "application"; + wxString appNameStr(""); + if (wxTheApp) + { + appNameStr = wxTheApp->GetAppName(); + appName = (char*) (const char*) appNameStr; + wxLogDebug("----- Memory statistics of %s at %s -----", appName, WXSTRINGCAST wxNow() ); + } + else + { + wxLogDebug( "----- Memory statistics -----" ); + } + } + bool currentMode = GetDebugMode(); SetDebugMode(FALSE); @@ -700,7 +749,7 @@ bool wxDebugContext::PrintStatistics(bool detailed) wxDebugStatsStruct *list = NULL; - wxMemStruct *from = (checkPoint ? checkPoint->m_next : NULL); + wxMemStruct *from = (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL ); if (!from) from = wxDebugContext::GetHead (); @@ -708,7 +757,8 @@ bool wxDebugContext::PrintStatistics(bool detailed) for (st = from; st != 0; st = st->m_next) { void* data = st->GetActualData(); - if (detailed && (data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) +// if (detailed && (data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) + if (detailed && (data != (void*) wxLog::GetActiveTarget())) { char *className = "nonobject"; if (st->m_isObject && st->GetActualData()) @@ -730,7 +780,8 @@ bool wxDebugContext::PrintStatistics(bool detailed) stats->totalSize += st->RequestSize(); } - if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) +// if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) + if (data != (void*) wxLog::GetActiveTarget()) { totalSize += st->RequestSize(); if (st->m_isObject) @@ -744,20 +795,22 @@ bool wxDebugContext::PrintStatistics(bool detailed) { while (list) { - wxTrace("%ld objects of class %s, total size %ld\n", + wxLogDebug("%ld objects of class %s, total size %ld", list->instanceCount, list->instanceClass, list->totalSize); wxDebugStatsStruct *old = list; list = old->next; free((char *)old); } - wxTrace("\n"); + wxLogDebug(""); } SetDebugMode(currentMode); - wxTrace("Number of object items: %ld\n", noObjectNodes); - wxTrace("Number of non-object items: %ld\n", noNonObjectNodes); - wxTrace("Total allocated size: %ld\n", totalSize); + wxLogDebug("Number of object items: %ld", noObjectNodes); + wxLogDebug("Number of non-object items: %ld", noNonObjectNodes); + wxLogDebug("Total allocated size: %ld", totalSize); + wxLogDebug(""); + wxLogDebug(""); return TRUE; #else @@ -767,8 +820,8 @@ bool wxDebugContext::PrintStatistics(bool detailed) bool wxDebugContext::PrintClasses(void) { - if (!HasStream()) - return FALSE; +// if (!HasStream()) +// return FALSE; if (TRUE) { @@ -778,31 +831,48 @@ bool wxDebugContext::PrintClasses(void) { appNameStr = wxTheApp->GetAppName(); appName = (char*) (const char*) appNameStr; - wxTrace("Classes in %s:\n\n", appName); + wxLogDebug("----- Classes in %s -----", appName); } } int n = 0; - wxClassInfo *info = wxClassInfo::first; - while (info) + wxNode *node; + wxClassInfo *info; + + wxClassInfo::sm_classTable->BeginFind(); + node = wxClassInfo::sm_classTable->Next(); + while (node) { + info = (wxClassInfo *)node->Data(); if (info->GetClassName()) { - wxTrace("%s ", info->GetClassName()); - - if (info->GetBaseClassName1() && !info->GetBaseClassName2()) - wxTrace("is a %s", info->GetBaseClassName1()); - else if (info->GetBaseClassName1() && info->GetBaseClassName2()) - wxTrace("is a %s, %s", info->GetBaseClassName1(), info->GetBaseClassName2()); - if (info->objectConstructor) - wxTrace(": dynamic\n"); - else - wxTrace("\n"); + wxString msg(info->GetClassName()); + msg += " "; + + if (info->GetBaseClassName1() && !info->GetBaseClassName2()) + { + msg += "is a "; + msg += info->GetBaseClassName1(); + } + else if (info->GetBaseClassName1() && info->GetBaseClassName2()) + { + msg += "is a "; + msg += info->GetBaseClassName1() ; + msg += ", "; + msg += info->GetBaseClassName2() ; + } + if (info->GetConstructor()) + msg += ": dynamic"; + + wxLogDebug(msg); } - info = info->next; + node = wxClassInfo::sm_classTable->Next(); n ++; } - wxTrace("\nThere are %d classes derived from wxObject.\n", n); + wxLogDebug(""); + wxLogDebug("There are %d classes derived from wxObject.", n); + wxLogDebug(""); + wxLogDebug(""); return TRUE; } @@ -819,7 +889,7 @@ int wxDebugContext::Check(bool checkAll) { int nFailures = 0; - wxMemStruct *from = (checkPoint ? checkPoint->m_next : NULL); + wxMemStruct *from = (checkPoint ? checkPoint->m_next : (wxMemStruct*)NULL ); if (!from || checkAll) from = wxDebugContext::GetHead (); @@ -836,16 +906,21 @@ int wxDebugContext::Check(bool checkAll) // Count the number of non-wxDebugContext-related objects // that are outstanding -int wxDebugContext::CountObjectsLeft(void) +int wxDebugContext::CountObjectsLeft(bool sinceCheckpoint) { int n = 0; - - wxMemStruct *from = wxDebugContext::GetHead (); + + wxMemStruct *from = NULL; + if (sinceCheckpoint && checkPoint) + from = checkPoint->m_next; + else + from = wxDebugContext::GetHead () ; for (wxMemStruct * st = from; st != 0; st = st->m_next) { void* data = st->GetActualData(); - if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) +// if ((data != (void*)m_debugStream) && (data != (void*) m_streamBuf)) + if (data != (void*) wxLog::GetActiveTarget()) n ++; } @@ -860,7 +935,7 @@ int wxDebugContext::CountObjectsLeft(void) // We'll only do malloc and free for the moment: leave the interesting // stuff for the wxObject versions. -#if WXDEBUG && USE_GLOBAL_MEMORY_OPERATORS +#if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS #ifdef new #undef new @@ -884,7 +959,28 @@ void * operator new (size_t size, char * fileName, int lineNum) #endif } -#if !( defined (_MSC_VER) && (_MSC_VER <= 1000) ) +// Added JACS 25/11/98 +void * operator new (size_t size) +{ +#ifdef NO_DEBUG_ALLOCATION + return malloc(size); +#else + return wxDebugAlloc(size, NULL, 0, FALSE); +#endif +} + +#if wxUSE_ARRAY_MEMORY_OPERATORS +void * operator new[] (size_t size) +{ +#ifdef NO_DEBUG_ALLOCATION + return malloc(size); +#else + return wxDebugAlloc(size, NULL, 0, FALSE, TRUE); +#endif +} +#endif + +#if wxUSE_ARRAY_MEMORY_OPERATORS void * operator new[] (size_t size, char * fileName, int lineNum) { #ifdef NO_DEBUG_ALLOCATION @@ -904,7 +1000,23 @@ void operator delete (void * buf) #endif } -#if !( defined (_MSC_VER) && (_MSC_VER <= 1000) ) +// VC++ 6.0 +#if _MSC_VER >= 1200 +void operator delete(void* pData, char* /* fileName */, int /* lineNum */) +{ +// ::operator delete(pData); + // JACS 21/11/1998: surely we need to call wxDebugFree? + wxDebugFree(pData, FALSE); +} +// New operator 21/11/1998 +void operator delete[](void* pData, char* /* fileName */, int /* lineNum */) +{ + wxDebugFree(pData, TRUE); +} +#endif + +#if wxUSE_ARRAY_MEMORY_OPERATORS + void operator delete[] (void * buf) { #ifdef NO_DEBUG_ALLOCATION @@ -918,7 +1030,7 @@ void operator delete[] (void * buf) #endif // TODO: store whether this is a vector or not. -void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bool isVect) +void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bool WXUNUSED(isVect) ) { // If not in debugging allocation mode, do the normal thing // so we don't leave any trace of ourselves in the node list. @@ -928,9 +1040,10 @@ void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bo return (void *)malloc(size); } - char * buf = (char *) malloc(wxDebugContext::TotSize (size)); + int totSize = wxDebugContext::TotSize (size); + char * buf = (char *) malloc(totSize); if (!buf) { - wxTrace("Call to malloc (%ld) failed.\n", (long)size); + wxLogDebug("Call to malloc (%ld) failed.", (long)size); return 0; } wxMemStruct * st = (wxMemStruct *)buf; @@ -969,7 +1082,7 @@ void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bo } // TODO: check whether was allocated as a vector -void wxDebugFree(void * buf, bool isVect) +void wxDebugFree(void * buf, bool WXUNUSED(isVect) ) { if (!buf) return; @@ -1021,7 +1134,7 @@ void wxTrace(const char *fmt ...) va_start(ap, fmt); -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wvsprintf(buffer,fmt,ap) ; #else vsprintf(buffer,fmt,ap) ; @@ -1035,7 +1148,7 @@ void wxTrace(const char *fmt ...) wxDebugContext::GetStream().flush(); } else -#ifdef __WINDOWS__ +#ifdef __WXMSW__ OutputDebugString((LPCSTR)buffer) ; #else fprintf(stderr, buffer); @@ -1053,7 +1166,7 @@ void wxTraceLevel(int level, const char *fmt ...) va_start(ap, fmt); -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wvsprintf(buffer,fmt,ap) ; #else vsprintf(buffer,fmt,ap) ; @@ -1067,14 +1180,14 @@ void wxTraceLevel(int level, const char *fmt ...) wxDebugContext::GetStream().flush(); } else -#ifdef __WINDOWS__ +#ifdef __WXMSW__ OutputDebugString((LPCSTR)buffer) ; #else fprintf(stderr, buffer); #endif } -#else // USE_MEMORY_TRACING && WXDEBUG +#else // wxUSE_MEMORY_TRACING && defined(__WXDEBUG__) void wxTrace(const char *WXUNUSED(fmt) ...) { }