Skip to content

memory leak when meshes update #263

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
slahser1992 opened this issue Jan 4, 2020 · 4 comments
Closed

memory leak when meshes update #263

slahser1992 opened this issue Jan 4, 2020 · 4 comments

Comments

@slahser1992
Copy link

👋 hi

I want to use react-three-fiber to draw something like this:

elements.map(element =>
<mesh material={elementMaterial}>
<shapeBufferGeometry>
</mesh>
)

when count of elements update, there will generate all new element again and can't dispose old geometry, I try to use useEffect to solve this problem but not work like this:

const map = () => { useEffect(() => { return function ref.current.geometry.dispose() }, []); }

@drcmda
Copy link
Member

drcmda commented Jan 6, 2020

it's missing a key. react won't unmount (and therefore dispose) it because it thinks you're just updating props.

elements.map(element =>
  <mesh key={element.uuid} material={element.material}>
    <geometry />
)

@slahser1992
Copy link
Author

it's missing a key. react won't unmount (and therefore dispose) it because it thinks you're just updating props.

elements.map(element =>
  <mesh key={element.uuid} material={element.material}>
    <geometry />
)

thanks for your reply and kind remind me, I forget this in my example.
but my code is:

const Section = ({ polygon, material, lanemarkers, id }) => {
	const ref = useRef();
	return (
		<group>
			<mesh
				visible
				material={material}
			>
				<shapeBufferGeometry
					ref={ref}
					attach={"geometry"}
					args={[new THREE.Shape(PointsToVector3Array(polygon.points))]}
				/>
					<mesh position={[0, 0, 1]}>
						{
							lanemarkers.map(lanemarker => {
								const linePoints = PointsToVector3Array(lanemarker.curve.points);
								return (
									<FatLine key={lanemarker.id} curve={linePoints}/>
								);
							})
						}
					</mesh>
			</mesh>
		</group>
	);
};

const HDMap = observer(() => {
	const { hdmapStore } = useStores();
	const sections = [];
	hdmapStore.sections.forEach((section) => {
		sections.push(section);
	});

	const [ref, material] = useResource();
	return (
		<>
			<meshBasicMaterial ref={ref} color={mapColor}/>
			{
				sections.map((section) =>
					<Section
						polygon={section.polygon}
						key={section.id}
						id={section.id}
						material={material}
						lanemarkers={section.lanemarkers}
					/>
				)}
			}
		</>
	);
});

this problem is not solved.

@drcmda
Copy link
Member

drcmda commented Jan 7, 2020

could you put this down into a codesandbox? a very reduced example that i can test

PS. this is an automatic mem-leak as well:

args={[new THREE.Shape(PointsToVector3Array(polygon.points))]}

means it's recreating buffer geom on every render.

const points = useMemo(() => new THREE.Shape(PointsToVector3Array(polygon.points)), [])
...
args={[points]}

@slahser1992
Copy link
Author

could you put this down into a codesandbox? a very reduced example that i can test

PS. this is an automatic mem-leak as well:

args={[new THREE.Shape(PointsToVector3Array(polygon.points))]}

means it's recreating buffer geom on every render.

const points = useMemo(() => new THREE.Shape(PointsToVector3Array(polygon.points)), [])
...
args={[points]}

thanks you very much, I realize the meaning of yours. but the problem still exists when I try this. so I guess the problem root is the dev-tool.so I replace react-three-fiber to three in my project, everything is ok.

@drcmda drcmda closed this as completed Mar 10, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants