#Cleanup component pattern to dispose native containers

1 messages · Page 1 of 1 (latest)

carmine herald
#

I'm looking for advice about the cleanup component pattern when cleaning up components that reference a native container that should be disposed.

Let's say we have

public struct ContainerComponent : IComponentData {
  public NativeList<int> container;
}

The docs (https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/components-cleanup-create.html) seem to suggest that another component that inherits ICleanupComponentData should be created. Though, to perform the cleanup, the other component should then also reference the container?
If that is the case then reallocating the container becomes cumbersome, also if the entity is destroyed before this cleanup component is created then we are leaking some memory.

Therefore I am thinking that ContainerComponent should directly inherit ICleanupComponentData, but then a tag component such as AliveContainerComponent becomes necessary for the cleanup system to differentiate alive/to-cleanup entities?

soft wind
#

The component that contains the resources to be cleaned up (the container in this case) should be the ICleanupComponent and another component type's existance is meant to signal if the entity has been created or destroyed

carmine herald
#

I see, thanks, that makes sense.
In the docs, the following is said:

Create a system that:

  1. Gets newly created entities of the target archetype. These are entities that contain the tag component but not the cleanup component.
  2. Adds the cleanup component to these entities.
    I don't really understand why the ICleanupComponent cannot be added right away to the entity (by being part of the prefab for instance)?
    Is it because often ICleanupComponent cannot be baked into prefabs? I suppose that you cannot bake a prefab with Native Containers
soft wind
#

Cleanup tends to imply some management of a resource. If we baked out that resource then cleanup wouldn't be necessary. Alternatively if you baked out an ICleanupComponent, what code is initializing the resource so it can later be cleaned up? Most of the time ICleanupComponent's contain pointers, which we don't serialize since they won't be valid when you deserialize, and some system needs to initialize them

carmine herald
#

That makes a lot of sense, thanks a lot 🙏

neon horizon
#

Doesn't IDisposable also work for the particular case of adding and removing components?

soft wind
#

If you mean does placing an IDispoable inside of a component or making a component IDisposable make it work like an ICleanupComponent, no.

We do not inspect components for IDisposable and auto dispose them when the entity is destroyed.

edgy marten
#

Just a note you can't rely on your normal job using cleanup components to dispose native containers as the job won't run leaving play mode (or if you say shut down world when you quit to menu)

#

You'd have to make sure you iterate in on destroy as well

carmine herald
edgy marten
#

i would say putting native containers on components is error prone and the fundamental problem 😛
(remember you can not put them on via baking, it will break in builds)

carmine herald
#

Yeah that is true. Though I guess it is necessary in some cases.
But maybe most of the time it is useful for singletons, and in that case the best might then be to do to allocate/disposal within the OnCreate/OnDestroy of a system?

edgy marten
#

yep!

#

much easier to manage on a singleton

nova flax
#

@edgy marten isn’t it quite necessary in certain cases to have arrays in a component

#

We use it in a couple of places

edgy marten
#

is that not the purpose of dynamicbuffer?

nova flax
#

Dynamic buffer can replace the use of all native containers? Usually we use it at the moment to keep track of cooldowns and which damages have been triggered etc during an ability execution

edgy marten
#

it can replace array/list

#

i have dynamichashmaps in my core library (though i rarely use them)

nova flax
#

Is it part of the entity? So I can iterate it as part of ije

edgy marten
#

but i'm of the belief you should never use native containers on entities that aren't singletons

nova flax
#

What’s the fundamental issue with it?

edgy marten
edgy marten
#

it breaks the safety system

nova flax
#

I use unsafelist kekwait

edgy marten
#

so what's the advantage of that over dynamicbuffer?

#

you can't bake it and you have to manually dispose it's memory somehow

nova flax
#

Never fully looked into dynamic buffer. Thanks this will help improve things a ton for us

#

Will slowly move it over I think

#

There was always something slightly uncomfortable using unsafe list