From: George Tasker Date: Thu, 23 Nov 2000 13:27:52 +0000 (+0000) Subject: Lots more memory/resource leak fixes. There are still more, but this solves about... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/3924dd226e469685d02fc2474a3fb149b1aeed2a?ds=inline Lots more memory/resource leak fixes. There are still more, but this solves about 75% of them. These fixes also speed up tex2rtf greatly. I can now build all the RTF version of the wxWindows manual in under 30 seconds, when before it took around 90 seconds. Curley Brace matching is turned back on again. I cannot find any places where it does not work. If you find one, let me know git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8773 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/utils/tex2rtf/src/htmlutil.cpp b/utils/tex2rtf/src/htmlutil.cpp index 487fb97a62..e41b0312a6 100644 --- a/utils/tex2rtf/src/htmlutil.cpp +++ b/utils/tex2rtf/src/htmlutil.cpp @@ -28,6 +28,10 @@ #include "tex2rtf.h" #include "table.h" + +extern wxHashTable TexReferences; + + extern void DecToHex(int, char *); void GenerateHTMLIndexFile(char *fname); diff --git a/utils/tex2rtf/src/rtfutils.cpp b/utils/tex2rtf/src/rtfutils.cpp index 735745b2c4..60e31552e8 100644 --- a/utils/tex2rtf/src/rtfutils.cpp +++ b/utils/tex2rtf/src/rtfutils.cpp @@ -58,6 +58,11 @@ extern FILE *WinHelpContentsFile; extern char *RTFCharset; // This is defined in the Tex2Any library and isn't in use after parsing extern char *BigBuffer; + + +extern wxHashTable TexReferences; + + // Are we in verbatim mode? If so, format differently. static bool inVerbatim = FALSE; @@ -3482,6 +3487,8 @@ bool RTFOnArgument(int macroId, int arg_no, bool start) // Convert points to TWIPS (1 twip = 1/20th of point) imageWidth = (int)(20*(tok1 ? ParseUnitArgument(tok1) : 0)); imageHeight = (int)(20*(tok2 ? ParseUnitArgument(tok2) : 0)); + if (imageDimensions) // glt + delete [] imageDimensions; return FALSE; } else if (start && (arg_no == 2 )) @@ -3556,6 +3563,8 @@ bool RTFOnArgument(int macroId, int arg_no, bool start) sprintf(buf, "Warning: could not find a BMP or WMF equivalent for %s.", filename); OnInform(buf); } + if (filename) // glt + delete [] filename; } else // linear RTF { @@ -5218,15 +5227,20 @@ bool RTFGo(void) { wxConcatFiles("header.rtf", "chapters.rtf", "tmp1.rtf"); Tex2RTFYield(TRUE); - if (FileExists(OutputFile)) wxRemoveFile(OutputFile); - wxString cwdStr, outputDirStr; + if (FileExists(OutputFile)) + wxRemoveFile(OutputFile); + + char *cwdStr; cwdStr = wxGetWorkingDirectory(); + + wxString outputDirStr; outputDirStr = wxPathOnly(OutputFile); + // Determine if the temp file and the output file are in the same directory, // and if they are, then just rename the temp file rather than copying // it, as this is much faster when working with large (multi-megabyte files) - if ((wxStrcmp(wxPathOnly(OutputFile),"") == 0) || // no path specified on output file - (wxStrcmp(wxGetWorkingDirectory(),wxPathOnly(OutputFile)) == 0)) // paths do not match + if ((wxStrcmp(outputDirStr.c_str(),"") == 0) || // no path specified on output file + (wxStrcmp(cwdStr,outputDirStr.c_str()) == 0)) // paths do not match { wxRenameFile("tmp1.rtf", OutputFile); } @@ -5234,6 +5248,7 @@ bool RTFGo(void) { wxCopyFile("tmp1.rtf", OutputFile); } + delete [] cwdStr; Tex2RTFYield(TRUE); wxRemoveFile("tmp1.rtf"); } diff --git a/utils/tex2rtf/src/tex2any.cpp b/utils/tex2rtf/src/tex2any.cpp index 26519cf47b..e18e545115 100644 --- a/utils/tex2rtf/src/tex2any.cpp +++ b/utils/tex2rtf/src/tex2any.cpp @@ -123,6 +123,10 @@ char *followedLinkColourString = NULL; bool combineSubSections = FALSE; bool htmlWorkshopFiles = FALSE; +extern int passNumber; + +extern wxHashTable TexReferences; + /* * International support */ @@ -191,6 +195,32 @@ TexMacroDef *VerbatimMacroDef = NULL; #define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++ + +TexRef::TexRef(char *label, char *file, char *section, char *sectionN) +{ + refLabel = copystring(label); + refFile = file ? copystring(file) : (char*) NULL; + sectionNumber = section ? copystring(section) : copystring("??"); + sectionName = sectionN ? copystring(sectionN) : copystring("??"); +} + +TexRef::~TexRef(void) +{ + delete [] refLabel; refLabel = NULL; + delete [] refFile; refFile = NULL; + delete [] sectionNumber; sectionNumber = NULL; + delete [] sectionName; sectionName = NULL; +} + + +CustomMacro::~CustomMacro() +{ + if (macroName) + delete [] macroName; + if (macroBody) + delete [] macroBody; +} + void TexOutput(char *s, bool ordinaryText) { int len = strlen(s); @@ -365,7 +395,7 @@ bool readInVerbatim = FALSE; // Within a verbatim, but not nec. verbatiminput // Switched this off because e.g. \verb${$ causes it to fail. There is no // detection of \verb yet. -#define CHECK_BRACES 0 +#define CHECK_BRACES 1 unsigned long leftCurly = 0; unsigned long rightCurly = 0; @@ -400,7 +430,7 @@ bool read_a_line(char *buf) if (rightCurly > leftCurly) { wxString errBuf; - errBuf.Printf("An extra right Curly brace ('}') was detected at line %l inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str()); + errBuf.Printf("An extra right Curly brace ('}') was detected at line %lu inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str()); OnError((char *)errBuf.c_str()); // Reduce the count of right curly braces, so the mismatched count @@ -1451,6 +1481,7 @@ int ParseMacroBody(char *macro_name, TexChunk *parent, bool TexLoadFile(char *filename) { + static char *line_buffer; stopRunning = FALSE; strcpy(TexFileRoot, filename); StripExtension(TexFileRoot); @@ -1459,10 +1490,12 @@ bool TexLoadFile(char *filename) TexPathList.EnsureFileAccessible(filename); + if (line_buffer) + delete line_buffer; #ifdef __WXMSW__ - static char *line_buffer = new char[600]; + line_buffer = new char[600]; #else - static char *line_buffer = new char[11000]; + line_buffer = new char[11000]; #endif Inputs[0] = fopen(filename, "r"); @@ -1818,17 +1851,27 @@ void TexCleanUp(void) BibliographyStyleString = copystring("plain"); DocumentStyleString = copystring("report"); MinorDocumentStyleString = NULL; -/* Don't want to remove custom macros after each pass. - SetFontSizes(10); - wxNode *node = CustomMacroList.First(); - while (node) - { - CustomMacro *macro = (CustomMacro *)node->Data(); - delete macro; - delete node; - node = CustomMacroList.First(); + + // gt - Changed this so if this is the final pass + // then we DO want to remove these macros, so that + // memory is not MASSIVELY leaked if the user + // does not exit the program, but instead runs + // the program again + if ((passNumber == 1 && !runTwice) || + (passNumber == 2 && runTwice)) + { +/* Don't want to remove custom macros after each pass.*/ + SetFontSizes(10); + wxNode *node = CustomMacroList.First(); + while (node) + { + CustomMacro *macro = (CustomMacro *)node->Data(); + delete macro; + delete node; + node = CustomMacroList.First(); + } } -*/ +/**/ TexReferences.BeginFind(); wxNode *node = TexReferences.Next(); while (node) diff --git a/utils/tex2rtf/src/tex2any.h b/utils/tex2rtf/src/tex2any.h index 37d25fb7b8..734c28dcd3 100644 --- a/utils/tex2rtf/src/tex2any.h +++ b/utils/tex2rtf/src/tex2any.h @@ -387,21 +387,10 @@ class TexRef: public wxObject char *refFile; // Reference filename (can be NULL) char *sectionNumber; // Section or figure number (as a string) char *sectionName; // name e.g. 'section' - TexRef(char *label, char *file, char *section, char *sectionN = NULL) - { - refLabel = copystring(label); - refFile = file ? copystring(file) : (char*) NULL; - sectionNumber = section ? copystring(section) : copystring("??"); - sectionName = sectionN ? copystring(sectionN) : copystring("??"); - } - ~TexRef(void) - { - delete[] refLabel; delete[] refFile; delete[] sectionNumber; delete[] sectionName; - } + TexRef(char *label, char *file, char *section, char *sectionN = NULL); + ~TexRef(void); }; -extern wxHashTable TexReferences; - /* * Add a reference * @@ -511,6 +500,7 @@ class CustomMacro: public wxObject else macroBody = NULL; } + ~CustomMacro(); }; bool ReadCustomMacros(char *filename); diff --git a/utils/tex2rtf/src/tex2rtf.cpp b/utils/tex2rtf/src/tex2rtf.cpp index e5ac8969ce..239e310c00 100644 --- a/utils/tex2rtf/src/tex2rtf.cpp +++ b/utils/tex2rtf/src/tex2rtf.cpp @@ -66,6 +66,27 @@ extern char *TexBibName; // Bibliography output file name extern char *TexTmpBibName; // Temporary bibliography output file name extern wxList ColourTable; extern TexChunk *TopLevel; +extern char *PageStyle; +extern char *BibliographyStyleString; +extern char *DocumentStyleString; +extern char *bitmapMethod; +extern char *backgroundColourString; +extern char *ContentsNameString; +extern char *AbstractNameString; +extern char *GlossaryNameString; +extern char *ReferencesNameString; +extern char *FiguresNameString; +extern char *TablesNameString; +extern char *FigureNameString; +extern char *TableNameString; +extern char *IndexNameString; +extern char *ChapterNameString; +extern char *SectionNameString; +extern char *SubsectionNameString; +extern char *SubsubsectionNameString; +extern char *UpNameString; + + #if wxUSE_HELP wxHelpController *HelpInstance = NULL; @@ -390,7 +411,10 @@ bool MyApp::OnInit() ReadCustomMacros((char*) (const char*) path); Go(); - if (runTwice) Go(); + if (runTwice) + { + Go(); + } #ifdef NO_GUI return 0; #else @@ -507,6 +531,28 @@ int MyApp::OnExit() RTFCharset = NULL; } + delete [] PageStyle; + delete [] BibliographyStyleString; + delete [] DocumentStyleString; + delete [] bitmapMethod; + delete [] backgroundColourString; + delete [] ContentsNameString; + delete [] AbstractNameString; + delete [] GlossaryNameString; + delete [] ReferencesNameString; + delete [] FiguresNameString; + delete [] TablesNameString; + delete [] FigureNameString; + delete [] TableNameString; + delete [] IndexNameString; + delete [] ChapterNameString; + delete [] SectionNameString; + delete [] SubsectionNameString; + delete [] SubsubsectionNameString; + delete [] UpNameString; + if (winHelpTitle) + delete[] winHelpTitle; + // TODO: this simulates zero-memory leaks! // Otherwise there are just too many... #ifndef __WXGTK__ diff --git a/utils/tex2rtf/src/texutils.cpp b/utils/tex2rtf/src/texutils.cpp index 35ff57107e..f3c25d42b7 100644 --- a/utils/tex2rtf/src/texutils.cpp +++ b/utils/tex2rtf/src/texutils.cpp @@ -432,7 +432,14 @@ void ReadTexReferences(char *filename) istr.get(ch); } section[i] = 0; + + // gt - needed to trick the hash table "TexReferences" into deleting the key + // strings it creates in the Put() function, but not the item that is + // created here, as that is destroyed elsewhere. Without doing this, there + // were massive memory leaks + TexReferences.DeleteContents(TRUE); TexReferences.Put(label, new TexRef(label, file, section, sectionName)); + TexReferences.DeleteContents(FALSE); } } } diff --git a/utils/tex2rtf/src/xlputils.cpp b/utils/tex2rtf/src/xlputils.cpp index bcbc4a0fb7..8b82f5ac7a 100644 --- a/utils/tex2rtf/src/xlputils.cpp +++ b/utils/tex2rtf/src/xlputils.cpp @@ -41,6 +41,10 @@ wxList hyperLinks(wxKEY_INTEGER); wxList hyperLabels(wxKEY_STRING); FILE *Index = NULL; + +extern wxHashTable TexReferences; + + void PadToTab(int tabPos) { int currentCol = GetCurrentColumn();