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 bd201102862a194f3f5ce669e0a3c8143eafc900.

Refs: https://github.com/nodejs/node/pull/20455

PR-URL: https://github.com/nodejs/node/pull/21607
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Anna Henningsen 2018-06-30 14:58:19 +02:00
parent 908518d9e3
commit 6e16ad7a63
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 19 additions and 0 deletions

View File

@ -90,6 +90,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
refs_(0), refs_(0),
gzip_id_bytes_read_(0), gzip_id_bytes_read_(0),
write_result_(nullptr) { write_result_(nullptr) {
MakeWeak();
} }

View File

@ -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}`);