A function to force dispose every leaked nativecollection

Sometimes there is exception or stopping execution while native collection was allocated but not disposed, such as using burst in edit mode. By any reason there was some point we know it safe to clean every nativecollection to prevent leaked. So please have some function to iterate all native collection that was allocated or just a function that force dispose every allocated nativecollection. And so we could avoid error A Native Collection has not been disposed

Is it possible? Or are there similar function already exist?

It’s not possible no - and honestly this isn’t a Burst issue even if it was.

Those errors are thrown from the editor so that you can fix the bug - in the player builds we don’t do that exhaustive checking deep into user code (because its very expensive to do!), and so you would just leak memory all over the place.

It don’t need to be check to dispose only the leaked. What I meant is to dispose every allocated along with the leaked. And at least I think we could do that in editor

still don’t think unity should do this. you should rather think ahead and dispose everything you allocate right away. if its an issue in editor it will also be an issue in game. sure it can happen but shouldn’t happen enough that you need to restart unity to free memory. i guess it will even teach you taking care of it more so you don’t have to restart

unity seems already way behind schedule on dots related things i’d rather not make them waste time implementing a garbage collector

First, you don’t understand the problem

The problem is even you code to dispose everything properly right away, the exception from anywhere between the allocation through the disposal will make the allocated collection become leaked

Second, in my case I use burst in editmode too. Not just playmode. So it could throw error while coding anytime. And whenever it leaked, errors become annoyingly cumulatively and I need to restart the editor to clear everything

Third. I didn’t propose that we need to have elaborate garbage collector on the job system. What I try to ask is a static function, helper method of some sort, that I could be call to dispose every allocated collection when I suspect there could be a leak and I think every job was completed. Such as when I change the scene

Fourth, I request this feature because I think it not really that hard. At least in editor the job system already always know every allocated collection, it could even tell us that there was leaked from error console that some was not disposed for some frame. But then I want the ability to disposing all those leaked things by calling some function instead of making another system to collect every allocated collection by myself

1 Like

The problem of things leaking after exceptions can usually be solved by proper use of using or finally. That’s the proper way to deal with this.

I don’t think that there would be much utility in an indiscriminate “dispose every allocated native container” method. If you use that method, you will inevitably eventually run into a situation where you actually want to make some other native container exempt from it. You won’t be able to do so, and will then be blocked.