Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix USL memory leak. #112

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions libusl/src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ Prototype::Prototype(Heap* heap):
Value(heap, 0)
{}

Prototype::~Prototype()
{
for (auto const& pair : members) {
delete pair.second;
}
std::for_each(scopes.begin(), scopes.end(), [](ScopePrototype* p) { delete p; });
}


void Prototype::addMethod(NativeCode* native)
{
ScopePrototype* scope = new ScopePrototype(static_cast<Heap*>(0), this); // TODO: GC
Expand All @@ -33,6 +42,7 @@ void Prototype::addMethod(NativeCode* native)
native->epilogue(scope);

members[native->name] = methodMember(scope);
scopes.push_back(scope);
}


Expand All @@ -41,6 +51,12 @@ ThunkPrototype::ThunkPrototype(Heap* heap, Prototype* outer):
outer(outer)
{}

ThunkPrototype::~ThunkPrototype()
{
for (Body::iterator it = body.begin(); it != body.end(); ++it)
delete *it;
}

/*
struct ScopeSize: NativeThunk
{
Expand Down Expand Up @@ -101,17 +117,17 @@ ScopePrototype::ScopePrototype(Heap* heap, Prototype* outer):
// members["prototype"] = &scopeMetaPrototype;
}

ScopePrototype::~ScopePrototype()
{
for (Body::iterator it = body.begin(); it != body.end(); ++it)
delete *it;
}


Scope::Scope(Heap* heap, ScopePrototype* prototype, Value* outer):
Thunk(heap, prototype, outer),
locals(prototype->locals.size(), 0)
{}

Scope::~Scope()
{
for (auto& local : locals) {
delete local;
}
}

/*
NativeThunk::NativeThunk(Prototype* outer, const std::string& name):
Expand Down
7 changes: 6 additions & 1 deletion libusl/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ extern Value nil;

struct ThunkPrototype;
struct NativeCode;
struct ScopePrototype;
struct Prototype: Value
{
typedef std::map<std::string, ThunkPrototype*> Members;
typedef std::vector<ScopePrototype*> Scopes;

Members members;
Scopes scopes;

Prototype(Heap* heap);
~Prototype();

void addMethod(NativeCode* native);

Expand Down Expand Up @@ -96,6 +100,7 @@ struct ThunkPrototype: Prototype
Body body;

ThunkPrototype(Heap* heap, Prototype* outer);
~ThunkPrototype();

virtual void dumpSpecific(std::ostream& stream) const
{
Expand Down Expand Up @@ -133,7 +138,6 @@ struct ScopePrototype: ThunkPrototype
Locals locals;

ScopePrototype(Heap* heap, Prototype* outer);
virtual ~ScopePrototype();
};

struct Scope: Thunk
Expand All @@ -144,6 +148,7 @@ struct Scope: Thunk
Locals locals;

Scope(Heap* heap, ScopePrototype* prototype, Value* outer);
~Scope();

virtual void dumpSpecific(std::ostream& stream) const
{
Expand Down
6 changes: 6 additions & 0 deletions libusl/src/usl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ Usl::Usl()
root = new Scope(&heap, prototype, 0);
}

Usl::~Usl()
{
delete root->prototype;
delete root;
}

void Usl::markGarbage() const
{
root->markForGC();
Expand Down
2 changes: 1 addition & 1 deletion libusl/src/usl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Scope;
struct Usl
{
Usl();
virtual ~Usl() {}
virtual ~Usl();

void markGarbage() const;
void collectGarbage();
Expand Down