Options: fix memory leaks (#928)

This commit is contained in:
slprime authored and GitHub committed 2026-05-06 22:36:07 +02:00
1 parent 2104184077
commit 41554f4c5b
13 files changed
+31 -21

No files matched your search

@@ -5,6 +5,7 @@ import java.util.LinkedList;
import net.minecraft.world.World;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.config.GuiOptionList;
import codechicken.nei.config.OptionCycled;
public class NEIInfo {
@@ -13,7 +14,8 @@ public class NEIInfo {
public static void load(World world) {
OptionCycled modeOption = (OptionCycled) NEIClientConfig.getOptionList().getOption("inventory.cheatmode");
modeOption.parent.synthesizeEnvironment(false);
// kept alive so the WeakReference in Option.slotRef isn't GC'd before copyGlobals/cycle use it
GuiOptionList env = modeOption.parent.synthesizeEnvironment(false);
if (!modeOption.optionValid(modeOption.value())) {
modeOption.copyGlobals();
modeOption.cycle();
@@ -93,7 +93,7 @@ public abstract class DataDumper extends Option {
public Rectangle4i dumpButtonSize() {
int width = 80;
return new Rectangle4i(slot.slotWidth() - width, 0, width, 20);
return new Rectangle4i(getSlot().slotWidth() - width, 0, width, 20);
}
public Rectangle4i modeButtonSize() {
@@ -64,7 +64,7 @@ public class GuiHighlightTips extends GuiScreenWidget {
@Override
public void keyTyped(char c, int keycode) {
if (keycode == Keyboard.KEY_ESCAPE || keycode == Keyboard.KEY_BACK) {
Minecraft.getMinecraft().displayGuiScreen(opt.slot.getGui());
Minecraft.getMinecraft().displayGuiScreen(opt.getSlot().getGui());
return;
}
super.keyTyped(c, keycode);
@@ -62,7 +62,7 @@ public class GuiItemIconDumper extends GuiScreen {
}
private void returnScreen(IChatComponent msg) {
Minecraft.getMinecraft().displayGuiScreen(opt.slot.getGui());
Minecraft.getMinecraft().displayGuiScreen(opt.getSlot().getGui());
NEIClientUtils.printChatMessage(msg);
}
@@ -98,7 +98,7 @@ public class GuiItemSorter extends GuiOptionPane {
@Override
public GuiScreen getParentScreen() {
return opt.slot.getGui();
return opt.getSlot().getGui();
}
@Override
@@ -247,7 +247,7 @@ public class GuiPanelSettings extends GuiScreenWidget {
public void keyTyped(char c, int keycode) {
if (keycode == Keyboard.KEY_ESCAPE || keycode == Keyboard.KEY_BACK) {
Minecraft.getMinecraft().displayGuiScreen(opt.slot.getGui());
Minecraft.getMinecraft().displayGuiScreen(opt.getSlot().getGui());
return;
}
@@ -1,5 +1,6 @@
package codechicken.nei.config;
import java.lang.ref.WeakReference;
import java.util.List;
import net.minecraft.client.Minecraft;
@@ -17,7 +18,11 @@ public abstract class Option {
.playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
}
public OptionScrollSlot slot;
private WeakReference<OptionScrollSlot> slotRef;
public OptionScrollSlot getSlot() {
return slotRef != null ? slotRef.get() : null;
}
public String namespace = null;
public final String name;
@@ -134,7 +139,7 @@ public abstract class Option {
* @return true if the gui is in world config mode
*/
public boolean worldConfig() {
return slot.getGui().worldConfig();
return getSlot().getGui().worldConfig();
}
/**
@@ -189,7 +194,7 @@ public abstract class Option {
}
public void onAdded(OptionScrollSlot slot) {
this.slot = slot;
this.slotRef = new WeakReference<>(slot);
}
public void onAdded(OptionList list) {
@@ -57,10 +57,10 @@ public abstract class OptionButton extends Option {
}
public Rectangle buttonSize() {
if (getPrefix() == null) return new Rectangle(0, 0, slot.slotWidth(), 20);
if (getPrefix() == null) return new Rectangle(0, 0, getSlot().slotWidth(), 20);
int width = Math.max(60, getStringWidth(getButtonText()) + 8);
return new Rectangle(slot.slotWidth() - width, 0, width, 20);
return new Rectangle(getSlot().slotWidth() - width, 0, width, 20);
}
public String getPrefix() {
@@ -85,7 +85,7 @@ public class OptionKeyBind extends OptionButton {
return false;
}
for (Option o : slot.options) {
for (Option o : getSlot().options) {
if (o instanceof OptionKeyBind && o != this && ((OptionKeyBind) o).getValue() == keyboard) {
return true;
}
@@ -131,13 +131,15 @@ public class OptionList extends OptionButton {
/**
* Adds this option to a temporary slot and gui for internal setting manipulation.
*/
public void synthesizeEnvironment(boolean world) {
getGui(null, this, world).addWidgets();
public GuiOptionList synthesizeEnvironment(boolean world) {
GuiOptionList gui = getGui(null, this, world);
gui.addWidgets();
return gui;
}
@Override
public boolean onClick(int button) {
openGui(slot.getGui(), slot.getGui().worldConfig());
openGui(getSlot().getGui(), getSlot().getGui().worldConfig());
return true;
}
@@ -131,6 +131,6 @@ public abstract class OptionStringSet extends Option {
}
public int buttonX() {
return slot.slotWidth() - (24 * options.size() - 4);
return getSlot().slotWidth() - (24 * options.size() - 4);
}
}
@@ -53,7 +53,7 @@ public class OptionTextField extends Option {
}
protected int getMaxInputWidth() {
return slot.slotWidth();
return getSlot().slotWidth();
}
public boolean isEnabled() {
@@ -64,9 +64,10 @@ public class OptionTextField extends Option {
public void draw(int mousex, int mousey, float frame) {
GuiDraw.drawString(getPrefix(), 10, 6, -1);
textField.w = Math
.max(60, Math.min(getMaxInputWidth(), slot.slotWidth() - GuiDraw.getStringWidth(getPrefix())) - 16);
textField.x = slot.slotWidth() - textField.w;
textField.w = Math.max(
60,
Math.min(getMaxInputWidth(), getSlot().slotWidth() - GuiDraw.getStringWidth(getPrefix())) - 16);
textField.x = getSlot().slotWidth() - textField.w;
textField.draw(mousex, mousey);
}
@@ -253,7 +253,7 @@ public class GuiPresetList extends GuiOptionPane {
@Override
public GuiScreen getParentScreen() {
return opt.slot.getGui();
return opt.getSlot().getGui();
}
@Override