Job.WithCode not allowed to access is?

Hi!

I am currently working my way into ECS and have written a job and am getting this error message:

ObjectDisposedException: The UNKNOWN_OBJECT_TYPE has been deallocated, it is not allowed to access it

I have now found out that it is this field that exists in my class:

private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();

My class looks like this, slightly shortened:

private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();

private bool UndergroundCheck(){
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}

}```

What follows is the error mentioned above.

```public partial class BuildingPlacementSystem : SystemBase {
private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();

private bool UndergroundCheck(){
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}

}```

Now I have tried it this way and get the same error:

```public partial class BuildingPlacementSystem : SystemBase {
private bool UndergroundCheck(){
NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}

}```

Can someone explain to me how the error occurs and what I can do?

Many thanks!

The default constructor for a NativeList leaves it in an uninitialized and unallocated state. You just need to pass in an Allocator when constructing it or before using it.

If you want this list to stick around across many frames, use Allocator.Persistent. If you only need the list for one frame, you can use this.WorldUpdateAllocator instead.

Thanks for the answer!

I now have the following in class.

private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>(Allocator.Persistent);

But I still get the same error.

EDIT:

If I implement the NativList within the job again, it works. However, I need the data from the NativList later outside the list. Is there a way to have the data outside the job?

EDIT 2:

OK! Apparently easier than I thought. At the end of the job, the NativeList in the job is simply transferred to the NativeList outside the job.

Job.WithCode(() => {
NativeList<Entity> _selectedEntityPlacementAreaJob = new NativeList<Entity>(Allocator.Temp);
_selectedEntityPlacementArea = _selectedEntityPlacementAreaJob;
}).WithoutBurst().Run();

Now I have the same error when deleting the NativeList.

_selectedEntityPlacementArea.Dispose();

ObjectDisposedException: Cannot access a disposed object.
Object name: ‘The NativeArray has been disposed, it is not allowed to access it’.

Can anyone help again? xD

That = seems to be the problem. Apparently you can’t just do the following or am I wrong?

_selectedEntityPlacementArea = _selectedEntityPlacementAreaJob;

How else could I get the data from the job to my NativeList?

Might need to see more of your code to understand what you are doing. But this link might be useful to learn how to pass data into and out of jobs: Scheduling background jobs with Job.WithCode | Entities | 1.2.3

While that link uses Allocator.TempJob, you can also use the WorldUpdateAllocator and pass collections allocated with that into jobs. You can read more about it here: World update allocator | Entities | 1.2.3

And here is a reference all of the different allocators and when to use each: Allocators overview | Entities | 1.2.3

The majority of the time you can just use one of these three:

  • Allocator.Persistent when you want to keep the collection around forever or multiple frames. Must be disposed manually.

  • WorldUpdateAllocator when you want to pass data around between jobs within the same frame. No need to dispose this manually.

  • Allocator.Temp when you need a super short lived collection and don’t need to pass it between jobs. Automatically disposed when it falls out of scope.

1 Like

Thank you very much, I’ll work through your links. Maybe it will work then. :slight_smile:

EDIT:

Yes, it works now, thanks again. :slight_smile: