Skip to content

Commit 96dae83

Browse files
addaleaxtargos
authored andcommitted
zlib: fix memory leak for unused zlib instances
An oversight in an earlier commit led to a memory leak in the untypical situation that zlib instances are created but never used, because zlib handles no longer started out their life as weak handles. The bug was introduced in bd20110. Refs: #20455 PR-URL: #21607 Reviewed-By: Tobias Nießen <[email protected]>
1 parent 9cd5c0e commit 96dae83

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/node_zlib.cc

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
9090
refs_(0),
9191
gzip_id_bytes_read_(0),
9292
write_result_(nullptr) {
93+
MakeWeak();
9394
}
9495

9596

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
// Flags: --expose-gc
3+
require('../common');
4+
const assert = require('assert');
5+
const zlib = require('zlib');
6+
7+
// Tests that native zlib handles start out their life as weak handles.
8+
9+
const before = process.memoryUsage().external;
10+
for (let i = 0; i < 100; ++i)
11+
zlib.createGzip();
12+
const afterCreation = process.memoryUsage().external;
13+
global.gc();
14+
const afterGC = process.memoryUsage().external;
15+
16+
assert((afterGC - before) / (afterCreation - before) <= 0.05,
17+
`Expected after-GC delta ${afterGC - before} to be less than 5 %` +
18+
` of before-GC delta ${afterCreation - before}`);

0 commit comments

Comments
 (0)