OpenTrespasser / JurassicParkTrespasser

A git-based fork of the Jurassic Park: Trespasser source code.
104 stars 25 forks source link

Resolve dangling link pointers of STextureQuadNodes #97

Open meekee7 opened 4 years ago

meekee7 commented 4 years ago

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:

meekee7 commented 4 years ago

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. TPass024

meekee7 commented 4 years ago

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