JurassicParkTrespasser
JurassicParkTrespasser copied to clipboard
Resolve dangling link pointers of STextureQuadNodes
CPackedRaster
manages trees of STextureQuadNode
objects. Those nodes are "linked" to each other via pointers. When a node is deleted, those link pointers are left dangling. When such a dangling pointer is dereferenced, this raises complaints when additional memory access validation (full page heap verification) is enabled.
A quick workaround is proposed in PR #95: cache deleted pointers to avoid using them again. However, that workaround is rather "dirty" and, to an extend, also incomplete. (What if a pointer becomes valid again? When can we remove from or clear the cache?)
A clean solution should be seeked. Key options include:
- use
std::weak_ptr
for the linkage pointers. However, the required switch tostd::shared_ptr
for all theSTextureQuadNode
pointers could have a significant performance penalty. - Reset link pointers when a node is about to be destroyed. This could require searching the entire tree, which is probably expensive.
- Store the linking data another way
A naive switch of the link pointers to std::weak_ptr
, retrieved by making STextureQuadNode
inherit from std::enable_shared_from_this
and calling weak_from_this
, does not work as expected. The effect is about the same as having the link pointer always be nullptr
: the terrain texture in close proximity gradually loses resolution until it becomes extremely very low-res.
For reference, I have put the aforementioned std::weak_ptr
attempt on a branch: https://github.com/meekee7/JurassicParkTrespasser/tree/texturepacksurface-weak-ptr
The relevant changes are in commit #aae263f.
https://github.com/meekee7/JurassicParkTrespasser/commit/aae263fa7cecd3f9505e453552dd1715a4b980f5