Skip to content

Game.Simulation.CarLaneSelectBuffer

Assembly:
Namespace: Game.Simulation

Type: struct

Base: System.ValueType

Summary:
A lightweight wrapper around a Unity.Collections.NativeArray used as a temporary buffer for car lane selection data. The buffer is lazily allocated with a fixed size of 64 elements using Allocator.Temp when Ensure() is first called. Note: the provided Dispose() implementation is empty in the source and therefore does not release the native memory — this is a bug/risk for memory leaks or misuse. Also be aware that Allocator.Temp memory is intended for very short-lived use (within the same frame).


Fields

  • private Unity.Collections.NativeArray<float> m_Buffer
    Holds the underlying native float array. It is created on-demand by Ensure(). Before creation its IsCreated property is false. Because the array is allocated with Allocator.Temp, it must be used and disposed within the same frame; persisting it across frames or into jobs that outlive the frame is unsafe.

Properties

  • None (no public properties in this type)

Constructors

  • public CarLaneSelectBuffer()
    Structs have an implicit parameterless constructor. m_Buffer starts in its default (non-created) state.

Methods

  • public Unity.Collections.NativeArray<float> Ensure()
    Creates and returns the internal buffer if it has not been created yet. Implementation details:
  • Allocates a NativeArray of length 64.
  • Uses Allocator.Temp and NativeArrayOptions.UninitializedMemory.
  • Returns the NativeArray (either newly created or previously existing).
  • Safe to call multiple times; subsequent calls return the same array.

Important considerations: - Allocator.Temp memory must not be stored across frames or used by jobs that execute later than the current frame. - Because UninitializedMemory is used, callers must ensure they write valid data before reading.

  • public void Dispose()
    Present in the source but empty — it does not dispose of m_Buffer. This means the NativeArray will not be freed by this method, which can lead to leaks or Unity native memory warnings. The typical expected behavior would be to call m_Buffer.Dispose() if m_Buffer.IsCreated.

Recommended fix (example) — see Usage Example.

Usage Example

// Original pattern (source):
CarLaneSelectBuffer buffer = default;
var arr = buffer.Ensure();
// use arr for the current frame...

// NOTE: The original Dispose() is empty; the buffer will not be freed.


// Recommended corrected pattern:
public struct CarLaneSelectBuffer
{
    private NativeArray<float> m_Buffer;

    public NativeArray<float> Ensure()
    {
        if (!m_Buffer.IsCreated)
            m_Buffer = new NativeArray<float>(64, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
        return m_Buffer;
    }

    public void Dispose()
    {
        if (m_Buffer.IsCreated)
        {
            m_Buffer.Dispose();
            m_Buffer = default;
        }
    }
}

// Usage in a single-frame context:
var buffer = new CarLaneSelectBuffer();
var arr = buffer.Ensure();
// populate arr...
// read arr...
buffer.Dispose(); // must be called before the end of the frame when using Allocator.Temp

// If the data must survive beyond the current frame or be used by background jobs,
// allocate with Allocator.TempJob or Allocator.Persistent and manage lifetime accordingly.

Additional notes for modders: - If you plan to pass the array to Unity.Jobs or store across frames, change the allocator to Allocator.TempJob or Allocator.Persistent and ensure correct disposal timing and job handles. - Always prefer disposing NativeArray when IsCreated is true to avoid native memory leaks; ensure Dispose is called on all code paths (use try/finally or a using-like pattern where appropriate). - When using UninitializedMemory, initialize elements before reading to avoid undefined behavior.