ElemeFE / ElemeFE/element

[Bug Report] messageBox beforeClose cb may cause memory leak

Open
#21,008 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Vue
Stars
54k
Forks
14.4k
PR merge metrics
No merged PRs in 30d

Description

### Element UI version
2.15.0

### OS/Browsers version
max OS 11.2.3 / Chrome 90.0.4430.93

### Vue version
2.6.12

### Reproduction Link
https://codesandbox.io/s/amazing-swanson-3dcce?file=/src/components/HelloWorld.vue

### Steps to reproduce
点击隐藏按钮,隐藏HelloWorld组件

### What is Expected?
因为HelloWorld使用了v-if,所以当点击confirm确认后,HelloWorld组件实例应该需要被卸载,被卸载后HelloWorld实例占用的内存应该被回收

### What is actually happening?
由于 `messageBox` 单例,且 `doClose` 后只是隐藏dom,所以实例是一直存在(保持全局单例),但是上述案例,点击按钮后 `HelloWorld` 实例隐藏符合预期,但是由于在 `beforeClose` 回调中使用了 `this` (`vm`),形成了一个闭包,有由于`messageBox`实例的存在,导致 `beforeClose` 形成的闭包无法被回收,结果也就导致 `HelloWorld` 组件实例无法被回收,联通子组件和元素无法被回收
> 注意,使用 `beforeClose` 且形成闭包后才有这个问题,使用 `promise.then` 的形式是没问题的。

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.