Scope: pop_scope() returns IntrusivePtr<>

Make sure unused scopes are freed to fix memory leaks.

The comment inside pop_scope() is now obsolete and I deleted it,
because this commit implements the real solution.

Note that this requires us to add a reference to the
push_existing_scope() call in dbg_eval_expr(), because it never owned
the reference.
This commit is contained in:
Max Kellermann 2020-02-26 05:48:38 +01:00
parent 8ea1d89529
commit 875bfc09a2
5 changed files with 13 additions and 9 deletions

View file

@ -20,6 +20,7 @@ using namespace std;
#include "Stmt.h"
#include "Frame.h"
#include "Func.h"
#include "IntrusivePtr.h"
#include "Scope.h"
#include "PolicyFile.h"
#include "Desc.h"
@ -968,7 +969,10 @@ Val* dbg_eval_expr(const char* expr)
const BroFunc* func = frame->GetFunction();
if ( func )
{
Ref(func->GetScope());
push_existing_scope(func->GetScope());
}
// ### Possibly push a debugger-local scope?

View file

@ -5,6 +5,7 @@
#include "Scope.h"
#include "Desc.h"
#include "ID.h"
#include "IntrusivePtr.h"
#include "Val.h"
#include "Reporter.h"
#include "module_util.h"
@ -204,21 +205,17 @@ void push_scope(ID* id, attr_list* attrs)
scopes.push_back(top_scope);
}
Scope* pop_scope()
IntrusivePtr<Scope> pop_scope()
{
if ( scopes.empty() )
reporter->InternalError("scope underflow");
scopes.pop_back();
Scope* old_top = top_scope;
// Don't delete the scope; keep it around for later name resolution
// in the debugger.
// ### SERIOUS MEMORY LEAK!?
// delete top_scope;
top_scope = scopes.empty() ? nullptr : scopes.back();
return old_top;
return {AdoptRef{}, old_top};
}
Scope* current_scope()

View file

@ -10,6 +10,7 @@
#include "BroList.h"
#include "TraverseTypes.h"
template <class T> class IntrusivePtr;
class ID;
class BroType;
class ListVal;
@ -89,7 +90,7 @@ extern void push_scope(ID* id, attr_list* attrs);
extern void push_existing_scope(Scope* scope);
// Returns the one popped off; it's not deleted.
extern Scope* pop_scope();
extern IntrusivePtr<Scope> pop_scope();
extern Scope* current_scope();
extern Scope* global_scope();

View file

@ -8,6 +8,7 @@
#include "Val.h"
#include "Expr.h"
#include "Func.h"
#include "IntrusivePtr.h"
#include "Stmt.h"
#include "Scope.h"
#include "Reporter.h"
@ -468,7 +469,7 @@ TraversalCode OuterIDBindingFinder::PostExpr(const Expr* expr)
void end_func(IntrusivePtr<Stmt> body)
{
auto ingredients = std::make_unique<function_ingredients>(pop_scope(), body.release());
auto ingredients = std::make_unique<function_ingredients>(pop_scope().release(), body.release());
if ( streq(ingredients->id->Name(), "anonymous-function") )
{

View file

@ -82,6 +82,7 @@
#include "Desc.h"
#include "Expr.h"
#include "Func.h"
#include "IntrusivePtr.h"
#include "Stmt.h"
#include "Val.h"
#include "Var.h"
@ -1250,7 +1251,7 @@ anonymous_function:
// Gather the ingredients for a BroFunc from the current scope
auto ingredients = std::make_unique<function_ingredients>(current_scope(), $5);
id_list outer_ids = gather_outer_ids(pop_scope(), $5);
id_list outer_ids = gather_outer_ids(pop_scope().get(), $5);
$$ = new LambdaExpr(std::move(ingredients), std::move(outer_ids));
}