fix: menu should not be garbage-collected when popuping (#21169)

* fix: retain menu when popuping

* test: menu should not be garbage-collected when popuping
This commit is contained in:
Cheng Zhao 2019-11-20 20:17:39 +09:00 committed by GitHub
parent ea23f18e94
commit 50f2d2b5ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 82 additions and 33 deletions

View file

@ -822,6 +822,31 @@ describe('Menu module', function () {
menu.closePopup()
})
})
it('prevents menu from getting garbage-collected when popuping', (done) => {
const menu = Menu.buildFromTemplate([{ role: 'paste' }])
menu.popup({ window: w })
// Keep a weak reference to the menu.
const v8Util = process.electronBinding('v8_util')
const map = (v8Util as any).createIDWeakMap() as any
map.set(0, menu)
setTimeout(() => {
// Do garbage collection, since |menu| is not referenced in this closure
// it would be gone after next call.
v8Util.requestGarbageCollectionForTesting()
setTimeout(() => {
// Try to receive menu from weak reference.
if (map.has(0)) {
map.get(0).closePopup()
done()
} else {
done('Menu is garbage-collected while popuping')
}
})
})
})
})
describe('Menu.setApplicationMenu', () => {