|
59 | 59 | import java.util.concurrent.*; |
60 | 60 | import java.util.concurrent.atomic.AtomicReference; |
61 | 61 | import java.util.stream.Collectors; |
| 62 | +import java.lang.ref.WeakReference; |
62 | 63 |
|
63 | 64 | import static javafx.application.Platform.runLater; |
64 | 65 | import static javafx.application.Platform.setImplicitExit; |
@@ -125,6 +126,9 @@ public void makeLaunchScript(File scriptFile) { |
125 | 126 | } |
126 | 127 |
|
127 | 128 | private void launch0() { |
| 129 | + // https://github.com/HMCL-dev/HMCL/pull/4121 |
| 130 | + PROCESSES.removeIf(it -> it.get() == null); |
| 131 | + |
128 | 132 | HMCLGameRepository repository = profile.getRepository(); |
129 | 133 | DefaultDependencyManager dependencyManager = profile.getDependency(); |
130 | 134 | AtomicReference<Version> version = new AtomicReference<>(MaintainTask.maintain(repository, repository.getResolvedVersion(selectedVersion))); |
@@ -209,7 +213,7 @@ private void launch0() { |
209 | 213 | } |
210 | 214 | }).thenAcceptAsync(process -> { // process is LaunchTask's result |
211 | 215 | if (scriptFile == null) { |
212 | | - PROCESSES.add(process); |
| 216 | + PROCESSES.add(new WeakReference<>(process)); |
213 | 217 | if (launcherVisibility == LauncherVisibility.CLOSE) |
214 | 218 | Launcher.stopApplication(); |
215 | 219 | else |
@@ -896,10 +900,10 @@ public void onExit(int exitCode, ExitType exitType) { |
896 | 900 |
|
897 | 901 | } |
898 | 902 |
|
899 | | - public static final Queue<ManagedProcess> PROCESSES = new ConcurrentLinkedQueue<>(); |
| 903 | + public static final Queue<WeakReference<ManagedProcess>> PROCESSES = new ConcurrentLinkedQueue<>(); |
900 | 904 |
|
901 | 905 | public static void stopManagedProcesses() { |
902 | 906 | while (!PROCESSES.isEmpty()) |
903 | | - Optional.ofNullable(PROCESSES.poll()).ifPresent(ManagedProcess::stop); |
| 907 | + Optional.ofNullable(PROCESSES.poll()).map(WeakReference::get).ifPresent(ManagedProcess::stop); |
904 | 908 | } |
905 | 909 | } |
0 commit comments