summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Heap/Heap.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-23 14:11:19 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-23 14:11:19 +0100
commitb2f005125debe74051bf74d773c15efa39bbb949 (patch)
tree04344634ed5f3d9b43d4d2bf3ee4a6780c8dae2c /Libraries/LibJS/Heap/Heap.cpp
parent6dc4b23e2f6dafa68930a0b5c5778485468e3fb4 (diff)
downloadserenity-b2f005125debe74051bf74d773c15efa39bbb949.zip
LibJS: Always collect all garbage when destroying Heap
When the Heap is going down, it's our last chance to run destructors, so add a separate collector mode where we simply skip over the marking phase and go directly to sweeping. This causes everything to get swept and all live cells get destroyed. This way, valgrind reports 0 leaks on exit. :^)
Diffstat (limited to 'Libraries/LibJS/Heap/Heap.cpp')
-rw-r--r--Libraries/LibJS/Heap/Heap.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp
index 4ad48b5a97..b3853680c4 100644
--- a/Libraries/LibJS/Heap/Heap.cpp
+++ b/Libraries/LibJS/Heap/Heap.cpp
@@ -51,6 +51,7 @@ Heap::Heap(Interpreter& interpreter)
Heap::~Heap()
{
+ collect_garbage(CollectionType::CollectEverything);
}
Cell* Heap::allocate_cell(size_t size)
@@ -72,11 +73,13 @@ Cell* Heap::allocate_cell(size_t size)
return cell;
}
-void Heap::collect_garbage()
+void Heap::collect_garbage(CollectionType collection_type)
{
- HashTable<Cell*> roots;
- gather_roots(roots);
- mark_live_cells(roots);
+ if (collection_type == CollectionType::CollectGarbage) {
+ HashTable<Cell*> roots;
+ gather_roots(roots);
+ mark_live_cells(roots);
+ }
sweep_dead_cells();
}