Fix world client memory leaks caused by block renderers (#88)

* make threadlocals final

* minor code fixes

* fix custom block renderers leaking world

* remove now useless threadlocal cleaning

* fix memory leak from rendererOld by just deleting the field

* fix ThreadLocals to work on dedicated server
This commit is contained in:
Alexdoru authored and GitHub committed 2026-04-20 11:45:05 +02:00
1 parent 2ae1f8ba93
commit ec610dc876
17 files changed
+114 -203

No files matched your search

@@ -2,6 +2,7 @@ package team.chisel.ctmlib;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
@@ -27,29 +28,38 @@ public class CTMRenderer implements ISimpleBlockRenderingHandler {
@Override
public void renderInventoryBlock(Block block, int metadata, int modelID, RenderBlocks renderer) {
GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
RenderBlocks rb = getContext(
renderer,
block,
Minecraft.getMinecraft().theWorld,
((ICTMBlock<?>) block).getManager(metadata),
metadata);
Drawing.drawBlock(block, metadata, rb);
GL11.glTranslatef(0.5F, 0.5F, 0.5F);
rb.unlockBlockBounds();
final WorldClient world = Minecraft.getMinecraft().theWorld;
final RenderBlocks rb = getContext(renderer, block, world, ((ICTMBlock<?>) block).getManager(metadata));
boolean clean = false;
if (rb.blockAccess == null) {
rb.blockAccess = world;
clean = true;
}
try {
Drawing.drawBlock(block, metadata, rb);
GL11.glTranslatef(0.5F, 0.5F, 0.5F);
rb.unlockBlockBounds();
} finally {
if (clean) rb.blockAccess = null;
}
}
@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId,
RenderBlocks rendererOld) {
RenderBlocks renderer) {
final int meta = world.getBlockMetadata(x, y, z);
final RenderBlocks rb = getContext(
renderer,
block,
world,
((ICTMBlock<?>) block).getManager(world, x, y, z, meta));
boolean clean = false;
if (rb.blockAccess == null) {
rb.blockAccess = world;
clean = true;
}
try {
int meta = world.getBlockMetadata(x, y, z);
RenderBlocks rb = getContext(
rendererOld,
block,
world,
((ICTMBlock<?>) block).getManager(world, x, y, z, meta),
meta);
rb.renderAllFaces = rendererOld.renderAllFaces;
rb.renderAllFaces = renderer.renderAllFaces;
boolean ret = rb.renderStandardBlock(block, x, y, z);
rb.unlockBlockBounds();
rb.renderAllFaces = false;
@@ -60,15 +70,15 @@ public class CTMRenderer implements ISimpleBlockRenderingHandler {
crashreportcategory.addCrashSection("Block name", GameRegistry.findUniqueIdentifierFor(block));
crashreportcategory.addCrashSection("Block metadata", world.getBlockMetadata(x, y, z));
throw new ReportedException(crashreport);
} finally {
if (clean) rb.blockAccess = null;
}
}
protected RenderBlocks getContext(RenderBlocks rendererOld, Block block, IBlockAccess world, ISubmapManager manager,
int meta) {
private RenderBlocks getContext(RenderBlocks rendererOld, Block block, IBlockAccess world, ISubmapManager manager) {
if (!rendererOld.hasOverrideBlockTexture() && manager != null) {
RenderBlocks rb = manager.createRenderContext(rendererOld, block, world);
if (rb != null && rb != rendererOld) {
rb.blockAccess = world;
if (rendererOld.lockBlockBounds) {
rb.overrideBlockBounds(
rendererOld.renderMinX,
@@ -78,10 +88,8 @@ public class CTMRenderer implements ISimpleBlockRenderingHandler {
rendererOld.renderMaxY,
rendererOld.renderMaxZ);
}
if (rb instanceof RenderBlocksCTM) {
RenderBlocksCTM rbctm = (RenderBlocksCTM) rb;
if (rb instanceof RenderBlocksCTM rbctm) {
rbctm.manager = rbctm.manager == null ? manager : rbctm.manager;
rbctm.rendererOld = rbctm.rendererOld == null ? rendererOld : rbctm.rendererOld;
}
return rb;
}
@@ -102,16 +102,14 @@ public class RenderBlocksCTM extends RenderBlocks {
XZ_HALF_Y(0.5, 1, 0.5);
// @formatter:on
private double x, y, z;
private final double x, y, z;
private Vert(double x, double y, double z) {
Vert(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
private static double u, v, xDiff, yDiff, zDiff, uDiff, vDiff;
void render(RenderBlocksCTM inst, ForgeDirection normal, int cacheID) {
final Tessellator tessellator = Tessellator.instance;
@@ -120,12 +118,15 @@ public class RenderBlocksCTM extends RenderBlocks {
tessellator.setBrightness(inst.lightingCache[cacheID]);
}
u = cacheID == 1 || cacheID == 2 ? inst.maxU : inst.minU;
v = cacheID < 2 ? inst.maxV : inst.minV;
double u = cacheID == 1 || cacheID == 2 ? inst.maxU : inst.minU;
double v = cacheID < 2 ? inst.maxV : inst.minV;
uDiff = inst.maxU - inst.minU;
vDiff = inst.maxV - inst.minV;
double uDiff = inst.maxU - inst.minU;
double vDiff = inst.maxV - inst.minV;
double xDiff;
double yDiff;
double zDiff;
if (inst.renderMinX + inst.renderMinY + inst.renderMinZ != 0
|| inst.renderMaxX + inst.renderMaxY + inst.renderMaxZ != 3) {
boolean uMin = u == inst.minU;
@@ -178,8 +179,8 @@ public class RenderBlocksCTM extends RenderBlocks {
YNEG_LB(ZERO, X_HALF, XZ_HALF, Z_HALF), YNEG_RB(X_HALF, X, Z_HALF_X, XZ_HALF), YNEG_RT(XZ_HALF, Z_HALF_X, XZ, X_HALF_Z), YNEG_LT(Z_HALF, XZ_HALF, X_HALF_Z, Z),
YPOS_LB(YZ, X_HALF_YZ, XZ_HALF_Y, Z_HALF_Y), YPOS_RB(X_HALF_YZ, XYZ, Z_HALF_XY, XZ_HALF_Y), YPOS_RT(XZ_HALF_Y, Z_HALF_XY, XY, X_HALF_Y), YPOS_LT(Z_HALF_Y, XZ_HALF_Y, X_HALF_Y, Y);
// @formatter:on
private Vert xmin, xmax, ymin, ymax;
private ForgeDirection normal;
private final Vert xmin, xmax, ymin, ymax;
private final ForgeDirection normal;
SubSide(Vert xmin, Vert ymin, Vert ymax, Vert xmax) {
this.xmin = xmin;
@@ -234,7 +235,6 @@ public class RenderBlocksCTM extends RenderBlocks {
protected float[] bluCache = new float[4];
public TextureSubmap submap;
public TextureSubmap submapSmall;
public RenderBlocks rendererOld;
public ISubmapManager manager;
protected int[][] lightmap = new int[3][3];
@@ -257,9 +257,6 @@ public class RenderBlocksCTM extends RenderBlocks {
tessellator.setColorOpaque_F(1.0F, 1.0F, 1.0F);
tessellator.addTranslation(x, y, z);
if (rendererOld != null && rendererOld.hasOverrideBlockTexture()) {
setOverrideBlockTexture(rendererOld.overrideBlockTexture);
}
inWorld = true;
boolean res = super.renderStandardBlock(block, x, y, z);
inWorld = false;
@@ -447,7 +444,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceXNeg(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 4);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 4);
fillLightmap(brightnessBottomRight, brightnessTopRight, brightnessTopLeft, brightnessBottomLeft);
fillColormap(colorRedBottomRight, colorRedTopRight, colorRedTopLeft, colorRedBottomLeft, redmap);
@@ -472,7 +469,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceXPos(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 5);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 5);
fillLightmap(brightnessTopLeft, brightnessBottomLeft, brightnessBottomRight, brightnessTopRight);
fillColormap(colorRedTopLeft, colorRedBottomLeft, colorRedBottomRight, colorRedTopRight, redmap);
@@ -497,7 +494,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceZNeg(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 2);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 2);
fillLightmap(brightnessBottomRight, brightnessTopRight, brightnessTopLeft, brightnessBottomLeft);
fillColormap(colorRedBottomRight, colorRedTopRight, colorRedTopLeft, colorRedBottomLeft, redmap);
@@ -522,7 +519,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceZPos(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 3);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 3);
fillLightmap(brightnessBottomLeft, brightnessBottomRight, brightnessTopRight, brightnessTopLeft);
fillColormap(colorRedBottomLeft, colorRedBottomRight, colorRedTopRight, colorRedTopLeft, redmap);
@@ -547,7 +544,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceYNeg(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 0);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 0);
fillLightmap(brightnessBottomLeft, brightnessBottomRight, brightnessTopRight, brightnessTopLeft);
fillColormap(colorRedBottomLeft, colorRedBottomRight, colorRedTopRight, colorRedTopLeft, redmap);
@@ -572,7 +569,7 @@ public class RenderBlocksCTM extends RenderBlocks {
if (!inWorld || submap == null) {
super.renderFaceYPos(block, 0, 0, 0, icon);
} else {
int tex[] = ctm.getSubmapIndices(blockAccess, bx, by, bz, 1);
int[] tex = ctm.getSubmapIndices(blockAccess, bx, by, bz, 1);
fillLightmap(brightnessTopRight, brightnessTopLeft, brightnessBottomLeft, brightnessBottomRight);
fillColormap(colorRedTopRight, colorRedTopLeft, colorRedBottomLeft, colorRedBottomRight, redmap);
@@ -16,8 +16,8 @@ public class RenderBlocksColumn extends RenderBlocks {
public IIcon iconTop;
private boolean inWorld;
private IIcon sides[] = new IIcon[6];
private CTM ctm = CTM.getInstance();
private final IIcon[] sides = new IIcon[6];
private final CTM ctm = CTM.getInstance();
public RenderBlocksColumn() {
super();