Skip to content

Commit f9fa639

Browse files
committed
fix: [PROD-14470] fix some risks of memory leaks
1 parent 3a5374b commit f9fa639

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/views/Simulation/components/Scene/Scene.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useTheme } from '@mui/styles';
55
import { useSimulationViewContext } from '../../SimulationViewContext';
66
import { DEFAULT_UPDATE_STATE } from '../../SimulationViewHook';
77
import { resetGraphHighlighting } from '../../utils/graphUtils';
8-
import { createApp, destroyApp, initApp, initMinimap, renderElements } from '../../utils/pixiUtils';
8+
import { createApp, destroyApp, initApp, initMinimap, renderElements, freeDisplayMemory } from '../../utils/pixiUtils';
99
import { ChartTimeline } from '../Charts';
1010
import { Minimap } from './Minimap';
1111

@@ -100,11 +100,7 @@ const Scene = () => {
100100
requiredUpdateStepsRef.current.layout ||
101101
requiredUpdateStepsRef.current.render
102102
) {
103-
if (sceneContainerRef.current) {
104-
sceneContainerRef.current.removeChildren().forEach((child) => {
105-
child.destroy({ children: true, texture: false, baseTexture: false });
106-
});
107-
}
103+
if (sceneContainerRef.current) freeDisplayMemory(sceneContainerRef.current);
108104

109105
const resetBounds = requiredUpdateStepsRef.current.all || requiredUpdateStepsRef.current.layout;
110106
renderElements(sceneContainerRef, graphRef, setSelectedElementId, settings, resetBounds);

src/views/Simulation/utils/MinimapContainer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ export class MinimapContainer extends Container {
1717
this.dragTarget = null;
1818
}
1919

20+
freeDisplayMemory() {
21+
this.children.forEach((child) => {
22+
child.destroy({
23+
children: true,
24+
texture: true,
25+
context: true,
26+
style: true,
27+
});
28+
});
29+
}
30+
2031
renderElements() {
21-
this.removeChildren().forEach((child) => child.destroy({ children: true }));
32+
this.freeDisplayMemory();
2233
this.miniSceneContainer = this.forgeContainer(this.sceneContainerRef.current);
2334

2435
this.addChild(this.miniSceneContainer);
@@ -53,7 +64,8 @@ export class MinimapContainer extends Container {
5364
createScreenCursor() {
5465
if (this.screenCursor != null) {
5566
this.removeChild(this.screenCursor);
56-
this.screenCursor.destroy({ children: true });
67+
this.screenCursor.destroy({ children: true, texture: true, context: true, style: true });
68+
this.screenCursor = null;
5769
}
5870

5971
this.screenCursor = new Graphics();

src/views/Simulation/utils/pixiUtils.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ const createProductionResourceContainer = (textures, name, isHighlighted, operat
203203
return container;
204204
};
205205

206-
function cubicBezier(p0, p1, p2, p3, t) {
206+
const cubicBezier = (p0, p1, p2, p3, t) => {
207207
const c = 3 * (p1 - p0);
208208
const b = 3 * (p2 - p1) - c;
209209
const a = p3 - p0 - c - b;
210210
return a * (t * t * t) + b * (t * t) + c * t + p0;
211-
}
211+
};
212212

213-
function drawArrow(stage, x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y, size = 12, isGrayedOut) {
213+
const drawArrow = (x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y, size = 12, isGrayedOut) => {
214214
const t = 0.5;
215215
const midX = cubicBezier(x1, cp1x, cp2x, x2, t);
216216
const midY = cubicBezier(y1, cp1y, cp2y, y2, t);
@@ -220,23 +220,22 @@ function drawArrow(stage, x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y, size = 12, isG
220220
const angle = Math.atan2(dy, dx);
221221

222222
const arrowGraphics = new Graphics();
223-
arrowGraphics.fill({ color: !isGrayedOut ? 0xffffff : HIDDEN_LINK_COLOR, alpha: 1 });
224223
arrowGraphics.poly([0, 0, -size, size / 2, -size, -size / 2]);
225-
arrowGraphics.endFill();
224+
arrowGraphics.fill({ color: !isGrayedOut ? 0xffffff : HIDDEN_LINK_COLOR, alpha: 1 });
226225
arrowGraphics.x = midX;
227226
arrowGraphics.y = midY;
228227
arrowGraphics.rotation = angle;
229-
stage.addChild(arrowGraphics);
230-
}
228+
return arrowGraphics;
229+
};
231230

232-
function cubicBezierDerivative(p0, p1, p2, p3, t) {
231+
const cubicBezierDerivative = (p0, p1, p2, p3, t) => {
233232
const c = 3 * (p1 - p0);
234233
const b = 3 * (p2 - p1) - c;
235234
const a = p3 - p0 - c - b;
236235
return 3 * a * t * t + 2 * b * t + c;
237-
}
236+
};
238237

239-
function drawDashedBezier(ctx, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2, dash = 4, gap = 4) {
238+
const drawDashedBezier = (ctx, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2, dash = 4, gap = 4) => {
240239
const segments = 80;
241240
let prev = { x: x1, y: y1 };
242241
let dist = 0;
@@ -269,14 +268,17 @@ function drawDashedBezier(ctx, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2, dash = 4,
269268
dist += segLength;
270269
}
271270
}
272-
}
271+
};
273272

274273
const createLinkGraphics = (links, setSelectedElementId, settings) => {
275274
const isLayoutHorizontal = settings?.orientation === 'horizontal';
276275
const spacingFactor = settings.spacing / 100.0;
277276

278277
return links.map((link) => {
278+
const graphicsContainer = new Container();
279279
const graphics = new Graphics();
280+
graphicsContainer.addChild(graphics);
281+
280282
graphics.eventMode = 'static';
281283
graphics.cursor = 'pointer';
282284

@@ -334,9 +336,7 @@ const createLinkGraphics = (links, setSelectedElementId, settings) => {
334336
);
335337

336338
graphics.lineTo(target.x - targetOffset.x, target.y - targetOffset.y);
337-
338-
drawArrow(
339-
graphics,
339+
const arrow = drawArrow(
340340
source.x + sourceOffset.x,
341341
source.y + sourceOffset.y,
342342
target.x - targetOffset.x,
@@ -348,6 +348,7 @@ const createLinkGraphics = (links, setSelectedElementId, settings) => {
348348
12,
349349
link.isGrayedOut
350350
);
351+
graphicsContainer.addChild(arrow);
351352
}
352353

353354
graphics.stroke({
@@ -360,7 +361,7 @@ const createLinkGraphics = (links, setSelectedElementId, settings) => {
360361
graphics.filters = link.isSelected && settings.enableGlowEffect ? [SELECTED_LINK_FILTER] : [];
361362
graphics.elementId = link.data.id;
362363

363-
return graphics;
364+
return graphicsContainer;
364365
});
365366
};
366367

@@ -411,9 +412,23 @@ const generateTextures = (app) => {
411412
return textures;
412413
};
413414

415+
export const freeDisplayMemory = (container) => {
416+
container.children.forEach((child) => {
417+
freeDisplayMemory(child);
418+
child.destroy({
419+
children: true,
420+
texture: true,
421+
context: true,
422+
style: true,
423+
});
424+
});
425+
};
426+
414427
export const renderElements = (sceneContainerRef, graphRef, setSelectedElementId, settings, resetBounds = true) => {
415428
if (!graphRef.current || !sceneContainerRef.current?.textures) return;
416429

430+
freeDisplayMemory(sceneContainerRef.current);
431+
417432
const { nodes, links } = graphRef.current;
418433
const textures = sceneContainerRef.current.textures;
419434

0 commit comments

Comments
 (0)