ObjectPool.Dispose() is missing. (2021+)

The docs show two methods, Clear() and Dispose(). Clear simply calls the destroy() method on each of the pooled items. Though the description in the docs is the same for both, it would be assumed that a memory-safe and thread-safe Dispose() would call Destroy() on all the items as well as Destroy the pool.

Why is there no Dispose() like in the docs? Can we just call Clear() and then Destroy(_pool)? Everything about the new Pool API warns about methods not being thread safe, so how do we know that in production a Clear() is not going to race the Destroy in some cases?

Anyone using this?

TIL that Unity has an ObjectPool.

Have you checked the docs for the method you cannot find? APIs do change over time.

Note: the IDisposable interface is typically where you find Dispose()… did that object change and no longer use IDisposable when it did before?

Yeah it’s brand new and I figured roll it while switching to Addressables too. It’s pretty simple and clean, and following GameDev Guru’s article, can be configured in about 15 minutes to replace your normal instantiation.

Anyway, the interface IObjectPool is missing Dispose, and I can’t see inside coremodule.dll so I can’t answer.

Oh and experimenting just now, it’s doesn’t extend Object, and so cannot be destroyed with Destroy(), so I don’t know what the hell Clear() is supposed to accomplish except destroy all the instantiated items (you can keep going on filling and get/release items from the pool as normal after Clear()).

edit: Oh wow, there’s a whole world of Finalize and Dispose in IDisposable that is above my head atm, managed resources and data structures and all that. I thought this was as simple as wrapping my pool in another class that implements the same interface but jeeze, I’m not so sure it’s that simple.

edit 2: I might have found the problem, it seems my implementation followed a tutorial that was written before the feature was even released, and implemented my own IObjectPool interface into a wrapper class, whereas Unity’s ObjectPool class implements IObjectPool and IDisposable already. Different tutorial than GameDev Guru. Anyway that seems to be the trick here.

1 Like

I assume “dispose” deletes all items, “clear” disables them.
Sounds quite useful to be able to disable all active pooled objects at once without having to keep a separate list of who is active.

Maybe just read the docs and answer your own question: https://docs.unity3d.com/ScriptReference/Pool.ObjectPool_1.html

Clear and Dispose both do the same thing.

A method to return all objects back into a pool doesn’t make much sense. It’s up to whatever is using something from the pool to return it when it’s done.

Man, what a necro, and just a day after this same topic:

https://forum.unity.com/threads/difference-between-objectpool-clear-and-dispose.1526851/#post-9526987

I was struggling to make sense of reality and time and space.

Assume? Aw man, you don’t have to do that. We have documentation! Use it!

I had assumed that the documentation was just incorrect, like, why would there be two differently named functions that do the exact same thing? It felt like they wrongly copy-pasted the same text twice by mistake.

1 Like

Because Clear() is part of the IObjectPool interface and Dispose() is part of the IDisposable interface. They do the same thing because frankly that’s the most sensible way to do things. This is one of those odd side effects as the result of using OOP that you just sort of have to live with from time-to-time.