#Error when freeing custom native container

1 messages · Page 1 of 1 (latest)

whole sequoia
#

Hello !
I'm trying to create a priority queue as a native container.
The queue is used in a job, it's allocated in the job constructor with TempJob memory (but it can be resized from within the job), used inside of the job and then disposed outside of the job when I'm done reading the output.
When it is deallocated, UnsafeUtility.Free throws an error :

UnityEngine.StackTraceUtility:ExtractStackTrace ()```. It even crashed the editor once 😦

After trying a few things, it seems the error only happens when I use the queue from a job (everything works fine if I use it from a classic MonoBehaviour script) and only if the queue needed to resize itself. And even when the error occurs, the priority queue works as expected.
I have no idea what is happening 🥲 Would anyone know what could be the cause of this issue ?
#

I use this method to resize the queue when needed, maybe I did something wrong here ?

private void AddHeap(Node node)
{
    if (heapLength + 1 > heapCapacity)
    {
        heapCapacity *= 2;
        void* newHeap = UnsafeUtility.Malloc(heapCapacity * UnsafeUtility.SizeOf<Node>(), UnsafeUtility.AlignOf<T>(), allocator);
        UnsafeUtility.MemCpy(newHeap, heap, heapLength * UnsafeUtility.SizeOf<Node>());
        UnsafeUtility.Free(heap, allocator);
        heap = newHeap;
    }
    UnsafeUtility.WriteArrayElement(heap, heapLength, node);
    heapLength++;
}
#

And this is the Dispose method :

public void Dispose()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
    CollectionHelper.DisposeSafetyHandle(ref m_Safety);
#endif
    UnsafeUtility.Free(heap, allocator);
    UnsafeUtility.Free(indexes, allocator);
}
gloomy kraken
#

Looks like the heap ptr is being updated in the job

#

But this won't be reflected outside the job because your container is a struct

#

So you're trying to dispose the old already disposed heap ptr

#

You need to store your changing allocations inside a constantly allocated struct

#

QueueData->heap