EditableMesh fails to destroy

I have an issue where my unfixed editable mesh wont get destroyed. This is causing issues because im now running out of memory way too fast and can only render in 4 chunks before the memory is completely out

the module;

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AssetService = game:GetService("AssetService")

-- Config modules
local config_modules = ReplicatedStorage:WaitForChild("Config")
local EnvironmentConfig = require(config_modules:WaitForChild("Environment"))
local VoxelsConfig = require(config_modules:WaitForChild("Voxels"))

-- Modules
local modules = ReplicatedStorage:WaitForChild("Modules")
local mixed_modules = modules:WaitForChild("Mixed")
local Chunks = require(mixed_modules:WaitForChild("Chunks"))

-- Chunks container folder
local chunks_container: Folder = workspace:FindFirstChild("Chunks") or Instance.new("Folder")
chunks_container.Name = "Chunks"
chunks_container.Parent = workspace

local VOXEL_VERTS = {
	Vector3.new(0, 0, 0),
	Vector3.new(1, 0, 0),
	Vector3.new(1, 1, 0),
	Vector3.new(0, 1, 0),
	Vector3.new(0, 0, 1),
	Vector3.new(1, 0, 1),
	Vector3.new(1, 1, 1),
	Vector3.new(0, 1, 1)
}

local VOXEL_TRIS = {
	{1, 3, 2}, {1, 4, 3},
	{5, 6, 7}, {5, 7, 8},
	{1, 5, 8}, {1, 8, 4},
	{2, 3, 7}, {2, 7, 6},
	{4, 8, 7}, {4, 7, 3},
	{1, 2, 6}, {1, 6, 5}
}

-- Module
local ChunkRenderer = {}

function ChunkRenderer.Render(ChunkID: number)

	local chunk_data = EnvironmentConfig.Chunks[ChunkID]
	if not chunk_data then return end

	local chunk_folder = chunks_container:FindFirstChild(tostring(ChunkID))
	if not chunk_folder then
		chunk_folder = Instance.new("Folder")
		chunk_folder.Name = tostring(ChunkID)
		chunk_folder.Parent = chunks_container
	end

	for _, obj in ipairs(chunk_folder:GetChildren()) do
		obj:Destroy()
	end

	local chunk_size = EnvironmentConfig.ChunkSize
	local voxel_size = EnvironmentConfig.VoxelSize

	local chunk_pos = Chunks.GetPositionFromID(ChunkID)
	local world_origin = Vector3.new(
		chunk_pos.X * chunk_size * voxel_size,
		chunk_pos.Y * chunk_size * voxel_size,
		chunk_pos.Z * chunk_size * voxel_size
	)
	local unfixed_mesh = AssetService:CreateEditableMesh()

	for voxel_pos, voxel_data in pairs(chunk_data) do
		local base_pos = voxel_pos * voxel_size

		local ids = {}
		for i, v in ipairs(VOXEL_VERTS) do
			ids[i] = unfixed_mesh:AddVertex(base_pos + v * voxel_size)
		end

		for _, tri in ipairs(VOXEL_TRIS) do
			unfixed_mesh:AddTriangle(ids[tri[1]], ids[tri[2]], ids[tri[3]])
		end
	end

	unfixed_mesh:RemoveUnused()

	local fixed_mesh: EditableMesh = AssetService:CreateEditableMeshAsync(Content.fromObject(unfixed_mesh), {FixedSize = true})
	unfixed_mesh:Destroy()
	
	local meshpart = AssetService:CreateMeshPartAsync(Content.fromObject(fixed_mesh), {
		CollisionFidelity = Enum.CollisionFidelity.Box,
		FluidFidelity = Enum.FluidFidelity.UseCollisionGeometry
	})
	meshpart:ApplyMesh(meshpart)
	meshpart.CFrame = CFrame.new(world_origin)
	meshpart.Name = "ChunkMesh"
	meshpart.CanCollide = false
	meshpart.CanQuery = false
	meshpart.Anchored = true
	meshpart.Parent = chunk_folder
	
	if unfixed_mesh then
		print("still here dawg")
		unfixed_mesh:Destroy()
		warn(unfixed_mesh)
-- these print, meaning the unfixed mesh is still here
	end
end

return ChunkRenderer

it got destroyed, but since your variable still references the instance it’s not collected by garbage collector and continues to live in memory

1 Like

how would i fix that ? i assume it would be like this

unfixed_mesh:Destroy()
unfixed_mesh = nil

but that doesnt work, so im not sure

how does it not work? btw you have meshpart and unfixed_mesh fixed_mesh variables, so nil all of them out

the variable does become nil but the unfixed mesh stays in memory. And i need the meshpart and the fixed_mesh, the only one i need to remove is the unfixed_mesh

show full code what you got rn

i can give a download to the placefile(theres not much in it, only afew short modules), the script i provided above is the entire script so far

the module above is called Chunk Renderer in client modules in replicated storage
Bits.rbxl (97.8 KB)

i ended up doing some mesh optimizations with surface culling and border culling and i can now render 25 chunks without hitting the memory limit, i was going to do these either way and the unfixed editable meshes still dont get destroyed properly so thats still an issue.

What do you mean dont get destroyed properly? After unfixed_mesh:Destroy() and unfixed_mesh = nil is executed mesh is destroyed completely. Also unfixed_mesh:RemoveUnused() does nothing here.

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AssetService = game:GetService("AssetService")

-- Config modules
local config_modules = ReplicatedStorage:WaitForChild("Config")
local EnvironmentConfig = require(config_modules:WaitForChild("Environment"))
local VoxelsConfig = require(config_modules:WaitForChild("Voxels"))

-- Modules
local modules = ReplicatedStorage:WaitForChild("Modules")
local mixed_modules = modules:WaitForChild("Mixed")
local Chunks = require(mixed_modules:WaitForChild("Chunks"))

-- Chunks container folder
local chunks_container: Folder = workspace:FindFirstChild("Chunks") or Instance.new("Folder")
chunks_container.Name = "Chunks"
chunks_container.Parent = workspace

local VOXEL_VERTS = {
	Vector3.new(0, 0, 0),
	Vector3.new(1, 0, 0),
	Vector3.new(1, 1, 0),
	Vector3.new(0, 1, 0),
	Vector3.new(0, 0, 1),
	Vector3.new(1, 0, 1),
	Vector3.new(1, 1, 1),
	Vector3.new(0, 1, 1)
}

local VOXEL_TRIS = {
	{1, 3, 2}, {1, 4, 3},
	{5, 6, 7}, {5, 7, 8},
	{1, 5, 8}, {1, 8, 4},
	{2, 3, 7}, {2, 7, 6},
	{4, 8, 7}, {4, 7, 3},
	{1, 2, 6}, {1, 6, 5}
}

-- Module
local ChunkRenderer = {}

function ChunkRenderer.Render(ChunkID: number)

	local chunk_data = EnvironmentConfig.Chunks[ChunkID]
	if not chunk_data then return end

	local chunk_folder = chunks_container:FindFirstChild(tostring(ChunkID))
	if not chunk_folder then
		chunk_folder = Instance.new("Folder")
		chunk_folder.Name = tostring(ChunkID)
		chunk_folder.Parent = chunks_container
	end

	for _, obj in ipairs(chunk_folder:GetChildren()) do
		obj:Destroy()
	end

	local chunk_size = EnvironmentConfig.ChunkSize
	local voxel_size = EnvironmentConfig.VoxelSize

	local chunk_pos = Chunks.GetPositionFromID(ChunkID)
	local world_origin = Vector3.new(
		chunk_pos.X * chunk_size * voxel_size,
		chunk_pos.Y * chunk_size * voxel_size,
		chunk_pos.Z * chunk_size * voxel_size
	)
	local unfixed_mesh = AssetService:CreateEditableMesh()
	if not unfixed_mesh then
		warn("We are out of memory") -- CreateEditableMesh returns nil when memory is out
		-- prevent errors
		return
	end

	for voxel_pos, voxel_data in pairs(chunk_data) do
		local base_pos = voxel_pos * voxel_size

		local ids = {}
		for i, v in ipairs(VOXEL_VERTS) do
			ids[i] = unfixed_mesh:AddVertex(base_pos + v * voxel_size)
		end

		for _, tri in ipairs(VOXEL_TRIS) do
			unfixed_mesh:AddTriangle(ids[tri[1]], ids[tri[2]], ids[tri[3]])
		end
	end
	
	local fixed_mesh: EditableMesh = AssetService:CreateEditableMeshAsync(Content.fromObject(unfixed_mesh), {FixedSize = true})
	unfixed_mesh:Destroy()
	unfixed_mesh = nil
	if not fixed_mesh then
		warn("We are out of memory") -- CreateEditableMeshAsync returns nil when memory is out
		-- prevent errors
		return
	end
	--local ids = unfixed_mesh:RemoveUnused()
	--for z,x in pairs(ids) do print(z,x) end
	local meshpart = AssetService:CreateMeshPartAsync(Content.fromObject(fixed_mesh), {
		CollisionFidelity = Enum.CollisionFidelity.Box,
		FluidFidelity = Enum.FluidFidelity.UseCollisionGeometry
	})
	meshpart:ApplyMesh(meshpart)
	meshpart.CFrame = CFrame.new(world_origin)
	meshpart.Name = "ChunkMesh"
	meshpart.CanCollide = false
	meshpart.CanQuery = false
	meshpart.Anchored = true
	meshpart.Parent = chunk_folder
end

return ChunkRenderer

yes it says its nil / destroyed, the mesh itself is still in memory which is the issue

i know, it was only there because i sampled the unfixed and fixed parts of code from another module i made

using the unfixed and fixed editable meshes, i can get up to 27 chunks loaded before hitting the memory limit. Removing that system and just using unfixed editable meshes i can load up to 38 chunks before hitting the memory limit. This means that the unfixed editable meshes dont leave the memory and since im using both fixed and unfixed it almost doubles the amount of memory used per chunk since the unfixed mesh doesnt get cleared (sorry if this is a bad explaination)

bro i closed my studio and reopened it and now it works fine
ig thats my fault for having studio open for like 3 days straight now :sob:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.