Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.lang.ref.WeakReference;

import static javafx.application.Platform.runLater;
import static javafx.application.Platform.setImplicitExit;
Expand Down Expand Up @@ -125,6 +126,9 @@ public void makeLaunchScript(File scriptFile) {
}

private void launch0() {
// https://github.com/HMCL-dev/HMCL/pull/4121
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment should be more descriptive about what this code does. Consider changing it to something like '// Clean up dead weak references to prevent memory leaks' to better explain the purpose of this cleanup operation.

Suggested change
// https://github.com/HMCL-dev/HMCL/pull/4121
// https://github.com/HMCL-dev/HMCL/pull/4121
// Clean up dead weak references to prevent memory leaks

Copilot uses AI. Check for mistakes.
PROCESSES.removeIf(it -> it.get() == null);

HMCLGameRepository repository = profile.getRepository();
DefaultDependencyManager dependencyManager = profile.getDependency();
AtomicReference<Version> version = new AtomicReference<>(MaintainTask.maintain(repository, repository.getResolvedVersion(selectedVersion)));
Expand Down Expand Up @@ -209,7 +213,7 @@ private void launch0() {
}
}).thenAcceptAsync(process -> { // process is LaunchTask's result
if (scriptFile == null) {
PROCESSES.add(process);
PROCESSES.add(new WeakReference<>(process));
if (launcherVisibility == LauncherVisibility.CLOSE)
Launcher.stopApplication();
else
Expand Down Expand Up @@ -896,10 +900,10 @@ public void onExit(int exitCode, ExitType exitType) {

}

public static final Queue<ManagedProcess> PROCESSES = new ConcurrentLinkedQueue<>();
public static final Queue<WeakReference<ManagedProcess>> PROCESSES = new ConcurrentLinkedQueue<>();

public static void stopManagedProcesses() {
while (!PROCESSES.isEmpty())
Optional.ofNullable(PROCESSES.poll()).ifPresent(ManagedProcess::stop);
Optional.ofNullable(PROCESSES.poll()).map(WeakReference::get).ifPresent(ManagedProcess::stop);
}
}