Fix some front-end memory leaks

This commit is contained in:
Geza Lore 2023-01-01 14:22:13 +00:00
parent 6ab47f799b
commit a168d593eb
6 changed files with 13 additions and 9 deletions

View File

@ -35,16 +35,13 @@ void V3Global::boot() {
m_rootp = new AstNetlist; m_rootp = new AstNetlist;
} }
void V3Global::clear() { void V3Global::shutdown() {
VL_DO_CLEAR(delete m_hierPlanp, m_hierPlanp = nullptr); // delete nullptr is safe
#ifdef VL_LEAK_CHECKS #ifdef VL_LEAK_CHECKS
if (m_rootp) VL_DO_CLEAR(m_rootp->deleteTree(), m_rootp = nullptr); if (m_rootp) VL_DO_CLEAR(m_rootp->deleteTree(), m_rootp = nullptr);
#endif #endif
} }
void V3Global::shutdown() {
VL_DO_CLEAR(delete m_hierPlanp, m_hierPlanp = nullptr); // delete nullptr is safe
}
void V3Global::checkTree() const { rootp()->checkTree(); } void V3Global::checkTree() const { rootp()->checkTree(); }
void V3Global::readFiles() { void V3Global::readFiles() {

View File

@ -126,7 +126,6 @@ public:
// CONSTRUCTORS // CONSTRUCTORS
V3Global() {} V3Global() {}
void boot(); void boot();
void clear();
void shutdown(); // Release allocated resources void shutdown(); // Release allocated resources
// ACCESSORS (general) // ACCESSORS (general)
AstNetlist* rootp() const VL_MT_SAFE { return m_rootp; } AstNetlist* rootp() const VL_MT_SAFE { return m_rootp; }

View File

@ -92,6 +92,7 @@ class V3PreProcImp;
# define yytext V3PreLextext # define yytext V3PreLextext
# define yyerror V3PreLexerror # define yyerror V3PreLexerror
# define yyerrorf V3PreLexerrorf # define yyerrorf V3PreLexerrorf
# define yylex_destroy V3PreLexlex_destroy
#endif #endif
#ifndef yyourleng #ifndef yyourleng
@ -117,6 +118,7 @@ extern void yyourtext(const char* textp, size_t size); // Must call with static
YY_BUFFER_STATE yy_create_buffer(FILE* file, int size); YY_BUFFER_STATE yy_create_buffer(FILE* file, int size);
void yy_switch_to_buffer(YY_BUFFER_STATE new_buffer); void yy_switch_to_buffer(YY_BUFFER_STATE new_buffer);
void yy_delete_buffer(YY_BUFFER_STATE b); void yy_delete_buffer(YY_BUFFER_STATE b);
int yylex_destroy();
//====================================================================== //======================================================================
@ -190,6 +192,7 @@ public: // Used only by V3PreLex.cpp and V3PreProc.cpp
m_streampStack.pop(); m_streampStack.pop();
} }
VL_DO_CLEAR(yy_delete_buffer(m_bufferState), m_bufferState = nullptr); VL_DO_CLEAR(yy_delete_buffer(m_bufferState), m_bufferState = nullptr);
yylex_destroy();
} }
// Called by V3PreLex.l from lexer // Called by V3PreLex.l from lexer

View File

@ -78,6 +78,10 @@ protected:
} }
} }
void shutdown() {
if (s_preprocp) VL_DO_DANGLING(delete s_preprocp, s_preprocp);
}
bool preproc(FileLine* fl, const string& modname, VInFilter* filterp, V3ParseImp* parsep, bool preproc(FileLine* fl, const string& modname, VInFilter* filterp, V3ParseImp* parsep,
const string& errmsg) { // "" for no error const string& errmsg) { // "" for no error
// Preprocess the given module, putting output in vppFilename // Preprocess the given module, putting output in vppFilename
@ -153,6 +157,7 @@ VInFilter* V3PreShellImp::s_filterp = nullptr;
// V3PreShell // V3PreShell
void V3PreShell::boot() { V3PreShellImp::s_preImp.boot(); } void V3PreShell::boot() { V3PreShellImp::s_preImp.boot(); }
void V3PreShell::shutdown() { V3PreShellImp::s_preImp.shutdown(); }
bool V3PreShell::preproc(FileLine* fl, const string& modname, VInFilter* filterp, bool V3PreShell::preproc(FileLine* fl, const string& modname, VInFilter* filterp,
V3ParseImp* parsep, const string& errmsg) { V3ParseImp* parsep, const string& errmsg) {
return V3PreShellImp::s_preImp.preproc(fl, modname, filterp, parsep, errmsg); return V3PreShellImp::s_preImp.preproc(fl, modname, filterp, parsep, errmsg);

View File

@ -33,6 +33,7 @@ class V3PreShell final {
// Static class for calling preprocessor // Static class for calling preprocessor
public: public:
static void boot(); static void boot();
static void shutdown();
static bool preproc(FileLine* fl, const string& modname, VInFilter* filterp, static bool preproc(FileLine* fl, const string& modname, VInFilter* filterp,
V3ParseImp* parsep, const string& errmsg); V3ParseImp* parsep, const string& errmsg);
static void preprocInclude(FileLine* fl, const string& modname); static void preprocInclude(FileLine* fl, const string& modname);

View File

@ -672,9 +672,6 @@ static void verilate(const string& argString) {
// Final writing shouldn't throw warnings, but... // Final writing shouldn't throw warnings, but...
V3Error::abortIfWarnings(); V3Error::abortIfWarnings();
// Cleanup memory for valgrind leak analysis
v3Global.clear();
FileLine::deleteAllRemaining();
} }
static string buildMakeCmd(const string& makefile, const string& target) { static string buildMakeCmd(const string& makefile, const string& target) {
@ -763,7 +760,9 @@ int main(int argc, char** argv) {
} }
// Explicitly release resources // Explicitly release resources
V3PreShell::shutdown();
v3Global.shutdown(); v3Global.shutdown();
FileLine::deleteAllRemaining();
UINFO(1, "Done, Exiting...\n"); UINFO(1, "Done, Exiting...\n");
} }