I’ve been having a problem with garbage data appearing in dynamic buffers on the client, and after finally getting something more reproducible I think I found the problem.
This is around line 576 in GhostReceiveSystem.c:
buf.ResizeUninitialized((int)newCapacity);
//Move buffer content around (because the slot size is changed)
var sourcePtr = (byte*)buf.GetUnsafePtr() + GhostSystemConstants.SnapshotHistorySize*slotCapacity;
var destPtr = (byte*)buf.GetUnsafePtr() + GhostSystemConstants.SnapshotHistorySize*newSlotCapacity;
for (int i=0;i<GhostSystemConstants.SnapshotHistorySize;++i)
{
destPtr -= newSlotCapacity;
sourcePtr -= slotCapacity;
UnsafeUtility.MemMove(destPtr, sourcePtr, slotCapacity);
}
slotCapacity = newSlotCapacity;
Both sourcePtr and destPtr doesn’t take into account the buffer header so the buffer is corrupted after the copy.
After adding the header size to those two pointers I stopped getting corrupted values:
var sourcePtr = (byte*)buf.GetUnsafePtr() + SnapshotDynamicBuffersHelper.GetHeaderSize()+ GhostSystemConstants.SnapshotHistorySize*slotCapacity;
var destPtr = (byte*)buf.GetUnsafePtr() + SnapshotDynamicBuffersHelper.GetHeaderSize()+ GhostSystemConstants.SnapshotHistorySize*newSlotCapacity;
Also, there is a variable baselineDynamicDataPtr in the same file that seems to refer to that buffer. It looks like it might be used after the buffer reallocation, but not sure.