JurassicParkTrespasser icon indicating copy to clipboard operation
JurassicParkTrespasser copied to clipboard

Resolve dangling link pointers of STextureQuadNodes

Open meekee7 opened this issue 4 years ago • 2 comments

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 to std::shared_ptr for all the STextureQuadNode 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

meekee7 avatar May 08 '20 19:05 meekee7

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 avatar Jun 10 '20 19:06 meekee7

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

meekee7 avatar Nov 14 '20 17:11 meekee7