Mass Battle Frameworkv1.20.0

指针还在,数据不是它的了 The Pointer Survives; the Data Does Not

MEMORY & THREAD SAFETY

为什么缓存一个片段指针,然后在加/减标记之后写它,会静默失效 · 2026-11-21Why caching a fragment pointer and writing through it after adding or removing a marker fails silently · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

那个坑:改值不生效,而且不报错 The Trap: The Write Does Nothing, and Says Nothing

取一个片段指针,给这个实体加个标记或去个标记,再用刚拿到的指针改值,值就是不变。不崩,不断言,一句提示都没有,安静地没生效。

You take a fragment pointer, add or remove a marker on the entity, then write through the pointer you just took. The value does not change. No crash, no assert, no message. It just silently does not take.

更麻烦的是,那个指针看上去还是好的,你甚至能从它读出一个值。只是那个值已经不属于这个实体了。这事没有玄机,从 ECS 存数据的方式直接推得出来。

The part that throws people off: the pointer still looks fine, and you can read a value through it. That value just no longer belongs to this entity. Nothing mysterious is happening; it follows straight from how an ECS lays out data.

片段住在块里的一列 Where a Fragment Lives: a Column in a Chunk

对象世界里,MyComponent->Health 那一块内存跟着对象走。ECS 不这么摆:同一个原型的实体全在同一段连续内存里,每个片段是里面的一列,第 N 个实体的生命值就挨着它的位置和速度。你拿到的"片段指针",是某个块里第 N 行、某一列的地址,就这些。

In an object world, the memory behind MyComponent->Health travels with the object. ECS does not arrange it that way: every entity of one archetype sits in the same run of contiguous memory, and each fragment is a column in it. Entity N's health sits next to entity N's position and velocity. The "fragment pointer" you hold is the address of row N, column X, inside one particular chunk. That is all it is.

实体落在哪个块,看它的原型。身上带哪些片段、哪些 tag,决定它的原型;原型决定块;块决定地址。

Which chunk an entity lands in comes from its archetype. The fragments and tags it carries decide the archetype, the archetype decides the chunk, the chunk decides the address.

加/减标记 = 换原型 = 搬家 Add or Remove a Marker = New Archetype = Moving House

给一个实体加 tag 或加片段,ECS 实际做的是把它搬到另一个原型去,把两个原型共有的片段拷过去(引擎文档的原话是 "moves an entity over to a new archetype by copying over fragments common to both archetypes")。搬完它就在另一块内存里,行号也换了。

Add a tag or a fragment to an entity and ECS moves it to another archetype, copying over the fragments both archetypes share (the engine's own wording for that operation: "moves an entity over to a new archetype by copying over fragments common to both archetypes"). Afterwards it sits in different memory, at a different row.

你手里那个旧指针指的,是搬走之前的位置。它接下来有三种下场,一种比一种难查:

The old pointer in your hand points at where the entity used to be. It has three fates from here, each harder to debug than the last:

旧指针指向The stale pointer points at你会看到什么What you see
已经释放或已被复用的内存Freed memory, or memory now reused 可能崩;更糟的是不崩,却悄悄改坏了另一个实体Possibly a crash; worse, no crash, and you have silently corrupted another entity
还留着旧值的"遗迹"Leftover bytes still holding the old value 能读能写,看着一切正常,可这个值已经不属于任何实体了。"改了不生效"就是这么来的Readable, writable, looks healthy, but the value belongs to no entity. This is the "the write did nothing" case
仍然合法的地址A still-valid address 没有任何断言能拦住它:地址合法、对齐合法、类型合法Nothing can assert on it: valid address, valid alignment, valid type

还有一个更容易错过的点:时机 One More Thing Easy to Miss: When the Move Happens

加/减 tag 很多时候是延迟命令。你调用它,只是把命令排进队列,真正生效要等当前阶段的冲刷点。所以在同一个处理器里,"我刚加了 tag"和"实体已经搬走"可以是两个时刻。

Adding or removing a tag is usually a deferred command. The call only queues it; it lands at the current stage's flush point. So within one processor, "I just added the tag" and "the entity has already moved" can sit at two different moments.

于是有两种踩法。在冲刷之前,实体还没搬,指针这会儿还真是好的,这也是它有时候看起来"能用"的原因。跨过冲刷之后,指针失效,后面所有写入都落在旧位置上。缓存只要跨过这条线就出事;同一个作用域里取一次用一次,从来不出事。

So there are two ways to trip. Before the flush the entity has not moved, and the pointer happens to be good, which is why it sometimes appears to "work". After the flush it is stale, and every write after that lands on the old spot. Cache across that line and you have a bug; take it and use it inside one scope and you never do.

由此得到的一条硬规则 The One Hard Rule

片段指针只活到这一次取用结束。它当不了成员变量,代表不了身份,存起来就错了。三条落地写法:

A fragment pointer is good for exactly one use. It is not a member and not an identity; storing it is a bug. Three practical forms:

一、跨帧只存句柄。要记住是哪个实体,就存实体句柄,下次用它重新取片段。二、指针别放进成员变量,也别从函数里返回出去。作用域一旦超过"取一次、用一次",就得假设中间可能发生过迁移。三、同一趟里取一次,用完就结束。连着改几个字段没问题,先把这些写完,中间别夹任何加/减标记、生成、销毁。

One: across frames, store the handle. When you need to remember which entity it is, keep the entity handle and re-fetch the fragment from it later. Two: no pointer in a member, no pointer returned out of scope. Once its scope outlives one take-and-use, assume a migration may have happened in between. Three: take it once per pass and be done with it. Several writes in a row are fine; do them first, with no add/remove, spawn or destroy in between.

判起来很直接:两次使用之间,有没有任何操作可能换掉原型?加标记、去标记、加片段、去片段、生成、销毁,摊上一条就是"地址可能变了"。答不上"绝对没有",就把指针重新取一遍。

The test is simple: between the two uses, could anything have changed the archetype? Add tag, remove tag, add fragment, remove fragment, spawn, destroy: hit any of those and "the address may have moved". If you cannot say "certainly not", fetch the pointer again.

一句话 In One Line

对象世界里指针跟着对象走;ECS 里指针跟着块里的位置走。位置会动,指针不动,它就安静地指着别的地方。别缓存指针,缓存句柄。

In an object world the pointer follows the object. In an ECS it follows a position inside a chunk. Positions move and pointers do not; they just quietly point somewhere else. Do not cache the pointer. Cache the handle.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels