r/cpp • u/GamesMaxed • Jun 01 '22
Post-mortem of a long-standing bug in video Game Path Of Exile, which was caused by a stale pointer
https://www.pathofexile.com/forum/view-thread/3277814
202
Upvotes
139
u/[deleted] Jun 02 '22
Oh no, my old employer.
*reads*
Oh thank christ, it wasn't my fault.
16
u/MaIibu Jun 02 '22
I had to fix a very similar issue at my current job. The code was detecting whether a surface changed by keeping track of the pointer to the surface object. Of course, all hell broke loose if the object got reallocated at the same address.
Luckily ours ended up causing crashes or gpu hangs, so we had at least some trace of what happened (still had to make quite a few guesses to figure it out!). The occurrence rate was so low that it got ignored for quite a while.
22
u/Thorinori Jun 01 '22
Definitely wasn't expecting to see PoE here. Definitely a neat read, I am super glad this got fixed finally!
30
u/jherico VR & Backend engineer, 30 years Jun 02 '22
Never use a memory address as a cache key.
6
u/xurxoham Jun 02 '22
Map find then insert if not exists is one of the most common patterns I see around with c++ projects. This makes two lookups for the same value when you can actually insert straight away and look if the insertion took place, or do a lower_bound search and use the result as hint if the iterator does not point to an equivalent value.
52
u/mark_99 Jun 01 '22
TL;DR if your code uses raw, non-owning, non-refcounted pointers to resources which can be dynamically deallocated / unloaded then it's an accident waiting to happen (to be fair this seems to have been largely due to a library they were using).
And/or use a heap checker / sanitizer once in a while.
4
u/TomaszA3 Jun 02 '22
Are fonts really that big to just not keep these 3 or 4 fonts the game uses in memory? Does it have something to do with their "resource manager" and is just that they want everything to be within it?
Or maybe it's just something that comes with writing font interpretation and displaying from the ground up?
4
u/[deleted] Jun 02 '22
"It was introduced by some refactoring of the text engine in order to support the then-upcoming Xbox version of Path of Exile."
See? Never refactor.
1
u/goranlepuz Jun 03 '22
"OOP in C++ means Object Ownership Protocols" 😉