Dynamic buffers become corrupted

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.

Hi Skiplist,

There were some bug in relation to dynamic buffer in 0.6-preview-7. One of them was exactly here (the memory was not moved correctly).
Has been already fixed (amoung others) in later version of NetCode. So sorry we didn’t ship yet a new update in a while.
The baselineDynamicDataPtr is used to decompressed the snapshot sent by server in case it is delta-compressed.
In relation to baselineDynamicDataPtr there was another problem (again, already fixed) that was causing reading memory from a release memory location, ending in garbage data inside the buffer.

I attached a diff with all the changes (not many) in case you need to locally fix also these.

7332817–890908–GhostReceiveSystem__diff.txt (6.39 KB)

1 Like

Thanks, that is very helpful. With this fixed I’m pretty happy with 0.6, but if we can get access to something newer soonish that would still be nice :slight_smile: