Skip to content

Commit

Permalink
zlib: fix memory leak for unused zlib instances
Browse files Browse the repository at this point in the history
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: nodejs#20455

PR-URL: nodejs#21607
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
addaleax committed Jul 3, 2018
1 parent 908518d commit 6e16ad7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
refs_(0),
gzip_id_bytes_read_(0),
write_result_(nullptr) {
MakeWeak();
}


Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-zlib-unused-weak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
// Flags: --expose-gc
require('../common');
const assert = require('assert');
const zlib = require('zlib');

// Tests that native zlib handles start out their life as weak handles.

const before = process.memoryUsage().external;
for (let i = 0; i < 100; ++i)
zlib.createGzip();
const afterCreation = process.memoryUsage().external;
global.gc();
const afterGC = process.memoryUsage().external;

assert((afterGC - before) / (afterCreation - before) <= 0.05,
`Expected after-GC delta ${afterGC - before} to be less than 5 %` +
` of before-GC delta ${afterCreation - before}`);

0 comments on commit 6e16ad7

Please sign in to comment.