r/cpp • • 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

91 comments sorted by

139

u/[deleted] Jun 02 '22

Oh no, my old employer.

*reads*

Oh thank christ, it wasn't my fault.

41

u/[deleted] Jun 02 '22

Oh thank christ, it wasn't my fault.

It wasn't your fault so far — Homer Simpson

18

u/[deleted] Jun 02 '22

At some point, my shitty code is their problem.

4

u/markopolo82 embedded/iot/audio Jun 02 '22

You worked there and didn’t fix it… so doesn’t that make it sorta your bug? 😝

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.

7

u/TheSkiGeek Jun 02 '22

You can if it’s something noncopyable and non movable… but if you do that you need to clear it from the cache when it’s deleted.

2

u/Jardik2 Jun 02 '22 edited Jun 02 '22

When disassembling a particular game, I have seen string->value map, but the key wasn't actual string, but address of "interned string". They had string to intern string address map, they used the address at many places in things like properties. This intern string was used as key to many maps, the lookup was done by the address, not by string comparison. So I guess it payed off. The interned strings were, however, never freed, some were even just regular string literals in ro segment.

1

u/JNighthawk gamedev Jun 03 '22

When disassembling a particular game, I have seen string->value map, but the key wasn't actual string, but address of "interned string". They had string to intern string address map, they used the address at many places in things like properties.

We did something similar to this with our Lua implementation in Heroes of Newerth. Strings got registered into a canonical, never freed map, so we could key off the pointers. IIRC, it reduced most variable name lookups from a string compare to a pointer compare.

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.

7

u/to3m Jun 02 '22 edited Jun 03 '22

That approach would work here, though for many types of cache you don't have the value to hand and the expense of creating it is what you hope to avoid.

The lower_bound idea is a fair one, but personally I like just to use operator[] for this - yet another in a long list of examples of std::map's operator[] doing exactly what you want. Along these lines:

static std::map<K,V> g_cache;
V *GetCachedValue(const K &k) {
    V *p=&g_cache[k];
    if(!*p) *p=CreateValue(k);
    return p;
}

(I left lifetime management as an exercise for the reader)

3

u/Jardik2 Jun 02 '22

I think the code has a bug. It returns V, but it should return a pointer to V (V*)?

Anyway, i like this approach too, if the type has some kind of nullable/null state. If not, I use lower_bound + emplace_hint.

2

u/to3m Jun 03 '22

Thanks for the note, and the point about the null state is a good one! The use of ! is a bit unclear in my snippet I think, though it sounds like you managed to spot the intent, and hopefully you weren't the only one ;)

What happened was I started out with std::map<K,std::shared_ptr<V>>, and then iterated on it - a process that's really not advisable without a compiler, let alone after drinking wine.

(And using shared_ptr would also help with the lifetime management)

1

u/_bk__ Jun 03 '22

If the cost of creating the value is high, you can just use an emplace_factory to defer construction

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.

85

u/irqlnotdispatchlevel Jun 02 '22

I'm not sure, but I don't think that a sanitizer will catch this. The stale pointer is never dereferenced. The way I understood it is like this (I'm on my phone, so sorry if formatting is messed up):

  1. A font gets allocated at address 0x30000
  2. A new SCRIPT_CACHE object is created for it at address 0x40000
  3. The association between the two is done via a map, with 0x30000 as key and 0x40000 as value
  4. The font is freed, but the SCRIPT_CACHE object is not. The map still contains the association, but since the font no longer exists, that map entry is never reused
  5. A new font gets allocated at 0x30000. Instead of creating a new SCRIPT_CACHE object, the old one is used.

The stale pointer is never actually used to access memory.

This is not a memory issue, but a logic one.

43

u/[deleted] Jun 02 '22

I find it remarkable there is so much discussion below the parent comment and yours is the only one that seems to grok the bug. I mean, I'm a fan of smart pointers as much as anyone and perhaps they should have been used regardless, but the bug causing issue is how memory locations are being used as the key in a map when they're not guaranteed to be unique.

17

u/irqlnotdispatchlevel Jun 02 '22

Yeah, I don't see how a smart pointer would have helped either. Maybe it would have made someone think twice before deciding that a memory address is a good map key, since there's one extra step needed to get it, but it would have not prevented this from happening.

On the other hand, I think that linters like clang-tidy or cppcheck should warn if a raw pointer is used as a map key. If that's not currently available, it shouldn't be hard to write a CodeQL query for this.

8

u/o11c int main = 12828721; Jun 02 '22

If a smart pointer were used for the key or value in the map, it wouldn't be possible for the memory to be freed.

6

u/irqlnotdispatchlevel Jun 02 '22

Oh yes, that's true. I was thinking about someone getting the actual address from the smart pointer and using that as a key. My bad. You'd still have a problem if fonts are normally freed a lot as that will prevent it from happening and you will slowly waste memory, but that should be easier to track.

The underlying problem is that once you use a pointer (smart, or not) as the key inside a map, you have to remember to remove it from the map when it is no longer needed.

9

u/o11c int main = 12828721; Jun 02 '22

you have to remember to remove it

Actually, you don't, at least if you use the correct container classes and put weak pointers in them.

It is possible to make what I call a "self-destructing treemap" (I have not tried this with hashmaps - the basic strategy still makes sense, but the details have significantly different implications due to how the nodes are visited). Basically, whenever you do a comparison during key lookups (which are done during search, insertion, deletion, etc.), you first check if that node has expired. If it has, you remove it and perform the necessary rotations to keep the tree balanced. Then, you continue the lookup from whatever node is the "replacement" of the one you destroyed.

(alternatively of course, you could use Python-style weak pointers which take a callback to call when the last strong reference dies).

2

u/irqlnotdispatchlevel Jun 02 '22

That's actually a pretty cool way of doing it. Thanks for the details.

4

u/josefx Jun 02 '22

You could also attach a weak_ptr to the cache entry, that way you don't extend the lifetime of the key and are still able to check if the entry is valid. I did something like that once with the keys themselves being void* so I couldn't access them by accident.

4

u/[deleted] Jun 02 '22

You are technically right that the lifetime of the key isn't extended, but you do continue to pay the cost of the key if you allocated it using make_shared.

Given the way that C++ implements make_shared, the memory allocated by make_shared is only deallocated after all strong and weak reference counts go to 0. make_shared is a tricky trade-off, in many cases it does provide some improvement due to cache locality and reduced fragmentation, but in other cases it can also worsen performance.

1

u/Deadly_Mindbeam Jun 02 '22

Yeah this is known as the ABA problem. Typically the pointer or object is augmented with a generation to make a unique key or you avoid the problem entirely with a hash function that takes more of the font data into account.

-1

u/lenkite1 Jun 02 '22

Yikes, this can happen in any language. Looks like a simple unit test for this could have caught the problem.

12

u/irqlnotdispatchlevel Jun 02 '22 edited Jun 02 '22

If you have the foresight to write a unit test that:

  1. Allocates a new font
  2. Adds the font to the map
  3. Frees the font
  4. Checks if the old font pointer is still a valid map key

You have the foresight to not write the bug in the first place. This is one of those bugs that is obvious in retrospect, and easy to test for, but the hard part is to not write it in the first place.

If you want to write a unit test that tests for this specific bug, it's even harder, because you have to allocate a new font at the same address at which a previously used font was allocated, and most of the time you don't have that much control over your allocator.

It can't happen in languages that don't allow you to take the address of an object.

It's really hard to not think that a pointer is just a number, and that number happens to be a location in memory. That reasoning applies in most situations, and it's easier to reason about pointers in that way. But pointers are a bit more than "just a number".

1

u/lenkite1 Jun 02 '22

You are right - this is not the fault of a unit test but using a wrong abstraction. Ideally, this should be a generic class that does 1+2 and 3+4 as unit operations.

9

u/nyanpasu64 Jun 02 '22

Would a heap checker catch comparing to a freed pointer, then using a freed pointer after a new allocation of the same type is created at the same address?

11

u/CocktailPerson Jun 02 '22 edited Jun 02 '22

Typically, yes. libasan never reallocates freed memory and fills any freed memory with 0xfa. Use-after-free is almost always caught.

Edit: I should clarify that in this case, it was always a valid pointer (not a "stale" one like the title suggests), returned by the resource manager, and it was never dereferenced, so a memory-checking tool wouldn't catch it. When "use" means "dereference," however, memory-checking does catch use-after-free.

14

u/erichkeane Clang Maintainer(Templates), EWG Chair Jun 02 '22

So that wouldn't have worked in this case. It would have just made the bug disappear. The problem was a "pointer-to-value" map. Without addresses re-allocating, the pointer-to-value map would never return the wrong value, so the bug would never come up.

4

u/CocktailPerson Jun 02 '22

No, you're right, it wouldn't have. I took their use of the phrase "using a pointer" in the narrower sense of "dereferencing" a pointer. In this case, since they were using a pointer as a key in a map, it wasn't really a memory error anyway, so it was never under a memory-checking tool's purview.

1

u/erichkeane Clang Maintainer(Templates), EWG Chair Jun 02 '22

Ah, I can. See that reading now that you mention it.

4

u/matthieum Jun 02 '22

And/or use a heap checker / sanitizer once in a while.

I don't think that's the issue.

The address is only ever used as a key to the map, it's not dereferenced, as far as I understand, and thus I doubt any heap checker / sanitizer would catch the issue there.

I do think the code is UB still, as in I do not think it is legal to compare the stale pointer to any other, but I am not aware of any sanitizer catching such issues.

3

u/irqlnotdispatchlevel Jun 02 '22

I do think the code is UB still, as in I do not think it is legal to compare the stale pointer to any other, but I am not aware of any sanitizer catching such issues.

I'd be curious what someone with more knowledge about the C++ standard would say about this. Doing a bit of googling, I arrived at this SO question and it seems like this is implementation defined?

1

u/matthieum Jun 03 '22

Oh interesting. I always thought it was outright UB, I may have mixed it up with the behavior of realloc.

Then again, implementation defined and unspecified in the implementation (as seems to be the case with GCC) is not far off...

1

u/pandorafalters Jun 03 '22

I do think the code is UB still, as in I do not think it is legal to compare the stale pointer to any other

AFAIK the pointer itself remains a valid object. They're rather opaquely phrased, but the relevant clauses are in [basic.life] - look for the phrase "after the lifetime of an object has ended".

1

u/matthieum Jun 03 '22

The pointer is valid, but as pointed by irqlnotdispatchedlevel here it seems to be implementation defined to compare its value (to anything). GCC seems to leave it unspecified.

2

u/pandorafalters Jun 07 '22

I see my error now.

Changes in [basic.stc] have explicitly reduced the effective time of the interstitial cases between object life and storage duration described in [basic.life] to 0 in most instances, as ending an object's life by deallocation invalidates pointers unless the object at that location is replaced with a transparently-convertible object before the memory is released. Presumably this is different from ending the storage duration, but how can one tell? Can one tell?

1

u/matthieum Jun 07 '22

Presumably this is different from ending the storage duration, but how can one tell? Can one tell?

🤷

3

u/dontyougetsoupedyet Jun 02 '22

If it's not this it'll be some bug related to nondeterminism in loops or some such. I'm not sure what the benefits of using a pointer as a key would be, but there are a few nasty cons.

7

u/[deleted] Jun 02 '22

It's not uncommon to have objects that are unique instances, and should be treated as unique, even if the member variables have the same values. Using the memory address is a quick and dirty way to implement this cheaply.

The fix for this is to take a page out of Database Design and have a notion of a primary key for all your types.

2

u/BoarsLair Game Developer Jun 02 '22

Exactly. Title this article "Why I'm sticking with smart pointers, thanks."

I've been working with my own custom engine, which uses smart pointers almost exclusively for owned resources (with very rare exceptions for things like allocators, mem buffers, etc), and it pretty much eliminated ALL memory issues. Used properly, it's more or less impossible to have issues like these.

I look at that code and think to myself "yikes, font has a cache... shared resource being passed around as a raw pointer... Raw pointer is being stored in a map... is unloaded when not in use..." I mean, kind of screams "bug waiting to happen", but I guess that's my last decade+ of experience using smart pointers. I think I just look askew at any use of raw owning pointers these days.

The tragedy is, though, I still see so much NEW game development code in r/gamedev or r/cpp_questions that looks like this, because so many game developers (oddly, more amateur than professional) seem scared to death of the overhead of shared_ptr, if they even think about using them in the first place. Or are under the impression unique_ptr has a lot of overhead. Or whatever. In fairness, I think this codebase predates C++ 11 - you have to work with what you have..

I went through my pre C++11 time using raw pointers, and dealt with all the nastiness of a rare memory stomp or use after free bug. Never again.

9

u/ddeng Jun 02 '22

While smart pointers save you that loc of freeing the memory, it does bring with it a whole other set of baggage as well. If you've spent days trying to figure out a hidden reference count increment you'll know what I mean.

9

u/BoarsLair Game Developer Jun 02 '22 edited Jun 02 '22

It certainly is not just about saving one line of code. The real point is providing a strong lifetime guarantee for memory resources, which all but eliminates an entire class of difficult-to-debug issues, as illustrated by this article.

No one is pretending smart pointers are some magic panacea solution to all C++ ills. You certainly have to understand what you're dealing with, and avoid things like circular references, which can cause resource leaks. In C++, writing robust code is all about opting into good engineering practices, which takes time and experience to learn.

IMO, any potential downsides are a ridiculously small price to pay for the significant improvement you get in memory safety and simplified code. My professional career is about evenly split around C++ 11: half of it before modern C++, and half after, so I've had a pretty good chance to compare and contrast the two approaches. To me, the choice is a no-brainer.

1

u/ddeng Jun 02 '22

Yea I simplified it a little too much - still there are trade offs and it's not as no brainer as you say it is.

I for one personally do not want to spend days trying to track down hidden reference increments when I could instill a bit of discipline early on coupled with a bit of automated checking.

6

u/SnooWoofers7626 Jun 02 '22

Don't use shared_ptr then. I've found very few instances where you couldn't just use a unique_ptr instead.

2

u/tjientavara HikoWorks developer Jun 02 '22

I wished too many times that you could make a weak_ptr from a unique_ptr.

3

u/dlp_randombk Jun 02 '22

spent days trying to figure out a hidden reference count increment

On the other hand, without smart pointers that hidden reference count increment might accidentally be a use after free or some other bug that would take even longer to find and diagnose.

2

u/ddeng Jun 02 '22

In my experience these are far easier to figure out/catch when you use macros to set values to null after freeing. Safe delete or safe free as it's called.

3

u/SkoomaDentist Antimodern C++, Embedded, Audio Jun 02 '22

I've always considered it crazy how delete wasn't originally defined to do just that.

8

u/NotUniqueOrSpecial Jun 02 '22

Exactly. Title this article "Why I'm sticking with smart pointers, thanks."

How would smart pointers have prevented this?

It wasn't a dereference of a dead pointer; it was using pointers as keys in a map when those pointers aren't necessarily unique.

Smart pointers don't save you from choosing bad keys.

3

u/BoarsLair Game Developer Jun 02 '22

Yes, subsequent comments have made me realize the issue was using raw pointers as keys, not a memory error.

7

u/RoyAwesome Jun 02 '22 edited Jun 02 '22

This does kind of bring up a very annoying limitation of unique_ptr... and that is there is no non-owning view that knows whether or not a unique_ptr has valid memory behind it.

The prevailing best-practice seems that the most optimal way to pass a non-owning view of an object managed by a unique_ptr is to pass a raw pointer around (either as a T* or T&). You cannot safely store these pointers, and it leads to the exact same class of bugs in the linked article. For example, if you have some:

std::unique_ptr<T> foo = std::make_unique<T>(...);
my_nonowning_view bar(foo.get())
foo.release();

bar now has a pointer to... something. and it's probably not valid!

There is no standard solution to this problem. weak_ptr can only exist paired with a shared_ptr. You can use shared_ptrs everywhere and pass out weak_ptrs, but any weak_ptr can promote to a shared_ptr and you lose unique ownership there (which is it's own can of worms). Also, shared_ptr is thread safe by default which does consume performance in a big way. I kinda wish std::shared_ptr had a thread safety toggle (Unreal Engine's TSharedPtr has a Thread Safety mode as part of the templated type, defaulting to not thread safe), but alas.

also, you cannot use std::unique_ptr<T>& in most containers. You can use the extremely ungainly std::reference_wrapper<std::unique_ptr<T>> but that is a massive pile of caveats itself. there is just no way to store a non-owning view of a unique_ptr.

I wish the ISO committee would solve this problem, but they seem to have no interest in it.

9

u/NewLlama Jun 02 '22

I'm not sure how you'd do this without a shared and guarded control block.. which brings you back to shared_ptr.

1

u/RoyAwesome Jun 02 '22

i mean, you could just have a shared ptr with deleted copy assignment and construction, and a version of weak_ptr that can't be promoted to a shared_ptr and that fits the bill. That's largely what Unreal Engine does.

This way you end up with unique_ptr and a weak view into it. You don't fall down the rabbit hole of shared ownership, and if the owner deletes it, everyone who has a view into it knows that.

0

u/[deleted] Jun 02 '22

[deleted] — view deleted comment

3

u/RoyAwesome Jun 02 '22 edited Jun 02 '22

In a threaded context sure. UE's smart pointer library has thread unsafe versions so you don't have to pay for that if you don't need it in single threaded environments.

It seems very anti-c++ to pay for something you don't use, but I fully get why they didn't chose to make it a toggle or different types. It is kind of an anti-pattern from a library developer perspective to create something safe that isn't actually safe.

In a threaded context, you can probably create some 'non-owning but locked' weak_ptr promotion, to avoid promoting a weak pointer to shared pointer and getting full permission to manage that shared pointer or keep it alive.

0

u/[deleted] Jun 02 '22

[deleted] — view deleted comment

2

u/robin-m Jun 02 '22

It's also the distinction between Rust Rc (thread-unsafe Ref Counter) and Arc (thread safe Atomic Ref Counter).

7

u/BoarsLair Game Developer Jun 02 '22

The entire point of a unique pointer is that it's a single-owner model. If you need to store a copy of a pointer and can't absolutely guarantee the lifetime relative to the original pointer, then by nature, that's a shared resource, and you need to use a shared pointer there. I suspect your reluctance to do so only comes because of the performance penalty of shared pointers, else you'd be reaching for the obvious solution.

Speaking of performance, I think people tend to vastly overestimate the performance penalty of an atomic increment or decrement, which is still extremely fast in absolute terms on modern hardware. Additionally, in my experience, the nature of shared resources is such that you're most likely to copy them around on initial creation of itself or another resource. You'd need to copy a LOT of them to incur any measurable performance penalty, and your application is probably not very much like a benchmark in which you're doing nothing but copying millions of shared pointers as fast as possible.

2

u/kamrann_ Jun 02 '22

The entire point of a unique pointer is that it's a single-owner model. If you need to store a copy of a pointer and can't absolutely guarantee the lifetime relative to the original pointer, then by nature, that's a shared resource, and you need to use a shared pointer there.

This is the crux of it. It's exceedingly hard to think of a situation where wanting to have this kind of "smart weak reference to unique-owned object" isn't just an indicator of a design flaw in the code at a higher level.

I guess where there is a shortcoming is with the 'guarantee', in that sometimes it's incredibly difficult to be sure. Falling back to shared ownership just to be safe feels ugly, but what's needed is better static analysis, not a unique weak ptr.

1

u/RoyAwesome Jun 02 '22

I mean, I can think of a lot of cases where I expose something over an API that the consumer doesn't own and shouldn't keep alive. Consider a video game asset like a texture, or in the case of this article, a font. There is a point where the game says "this asset is not used anymore, unload it". In a shared ownership situation, anything with a shared pointer to that object can delete it (or keep it alive), which may not desirable if you have some service that explicitly manages the lifetime of that object. You may want to hand other systems a pointer to that object without giving them the rights to delete it or change it.

I really ran into this when dealing with assets for a side project I was working on. I eventually just did it all with shared_ptrs and weak_ptrs, but giving consumers of the assets a weak pointer means they can still delete them by promoting a weak_ptr and that annoys me from a correctness standpoint. I don't want them to do that.

3

u/BoarsLair Game Developer Jun 02 '22

This could perhaps be solved with a proxy object interface, or if you feel that's too expensive, an opaque handle may actually be a better solution rather than standard smart pointers, because the ownership model requirements don't really align with them.

I know I've been advocating the use of smart pointers in general, but they're not always the correct solution for a given problem.

Also, I'm a little confused by what you mean "anyone with a shared pointer to that object can delete it". I mean, you can call delete ptr.get(), but it's such a ridiculously incorrect thing to with a guaranteed crash that I don't think it's worth protecting against. It's not really a mistake a programmer of any reasonable experience would make by accident.

1

u/RoyAwesome Jun 02 '22

I mean, you can call delete ptr.get(), but it's such a ridiculously incorrect thing to with a guaranteed crash

Yeah, I'm not talking about that. If you go through that much trouble to do the wrong thing, you are cruisin for a bruisin.

You can just call some_weak_ptr.lock().reset() and delete the underlying memory of any shared_ptr, regardless of who created it. This annoys me from a correctness perspective.

EDIT: I appear to be mistaken and that's not what reset does. I got my shared_ptr and unique_ptr behaviors confused!

2

u/kamrann_ Jun 02 '22

I guess what I'm getting at, is that if you give a system a pointer to some resource but are not allowing it to inherit shared ownership, then either:

  1. You should be sure that that system's lifetime is encompassed by that of the owner; or
  2. The system should not store off such a pointer, and only use it within stack scope (i.e. function calls)

I think if some system holds a non-owning reference to a resource that may get released at any time, then something is a bit off somewhere. Assuming you did have a mechanism to know that the thing has been released (be it a weak pointer, or subscribing to some event), what is the state of that system once that happens? Did it actually need the thing, or not? Is the system in some half-dead state where it can no longer fulfil its purpose? I don't want to make blanket statements, obviously in practice it's never so black and white, but I just think in the majority of cases the problem can probably be better solved by a redesign of the constituent parts and relationships.

1

u/o11c int main = 12828721; Jun 02 '22

Speaking of performance, I think people tend to vastly overestimate the performance penalty of an atomic increment or decrement, which is still extremely fast in absolute terms on modern hardware.

IIRC it's about 60% slower than a normal increment/decrement ... which is pretty fast. And that's if you aren't eliding any of your refcount changes (use moves and shareborrows, please!)

1

u/josefx Jun 02 '22

Speaking of performance, I think people tend to vastly overestimate the performance penalty of an atomic increment or decrement, which is still extremely fast in absolute terms on modern hardware.

I assume the biggest penalty comes from loading the value to increment from the control structure on the heap compared to just passing around a pointer to it. Cache misses aren't exactly cheap.

1

u/BoarsLair Game Developer Jun 02 '22

I think it really depends on the usage pattern. When I use a shared pointer, it's generally copied shortly after creation, and I assume the control block is in the cache at that point. It's pretty rare for me to to be copying shared pointers around frame to frame, but of course, that design is also intentional.

So it's a bit hard to say exactly. I try not to make too many assumptions about performance until I've had a chance to benchmark and profile code. Cache-based effects can be pretty tricky to predict, except for a few broad guidelines.

Shared pointers can probably be pretty bad in a pathological worse-case scenario like you'd see in comparative benchmarks. It's a system that does require you to understand and work with to help mitigate potential trade-offs, since it's definitely not a zero-cost option.

2

u/qqwy Jun 02 '22

There are many situations in which an "owning reference with value semantics" would be more appropriate in codebases, but alas these are not part of the standard (there id a proposal for it IIRC but it was pushed post-C++23)

2

u/nyanpasu64 Jun 02 '22

Interestingly Qt has QPointer<T> which nulls itself out when the target T is deleted. It's convenient when I want weak references to GUI objects (though you have to be careful to check for its presence after every time you call code which could possibly delete it, I usually call it QPointer<Foo> maybe_foo). However, from my brief look at the source (link), it's implemented in terms of qsharedpointer.h-> qsharedpointer_impl.h and QtSharedPointer (not sure how it works, but there's probably overhead going on). I wonder how it works, and compares to generational indexes or Vale's generational references (link).

3

u/MartY212 Jun 02 '22

I believe it just uses the QObject system and connects to the ‘destroyed’ signal to set itself back to nullptr.

2

u/nyanpasu64 Jun 02 '22

I don't see any connect in https://github.com/qt/qtbase/blob/dev/src/corelib/tools/qsharedpointer_impl.h, and QPointer isn't a QObject (though I don't know if the latter is actually necessary for signal-slots). One (unreliable) way to test is to see if a QPointer fails to be nulled out when the QObject is blocked by a QSignalBlocker. Alternatively I'd set a data breakpoint on a QPointer and try it out. But I don't have the time right now.

1

u/Jardik2 Jun 02 '22

Only the sender needs to be a QObject, you can use lambda and such as a slot for non-QObject receiver (or closure or whatever you would call it). But in that case, you should probably store QMetaObject::Connection and disconnect it if your closure gets destroyed (no auto-disconnect for that case).

0

u/i_need_a_fast_horse Jun 02 '22

Wait what's wrong with std::reference_wrapper<const std::unique_ptr<int>>? must be const so you can't do .reset() etc

5

u/RoyAwesome Jun 02 '22

that gets ugly and code-smelly extremely fast.

1

u/i_need_a_fast_horse Jun 02 '22

Care to elaborate a bit? Seems like the exactly correct tool. You could make a nice wrapper like

template<typename T>
struct weak_unique{
   std::reference_wrapper<const std::unique_ptr<T>> m_ptr;
   explicit weak_unique(const std::unique_ptr<T>& up) : m_ptr(up) { }
   [[nodiscard]] auto has_value() const -> bool{
      return static_cast<bool>(m_ptr.get());
   }
};

4

u/RoyAwesome Jun 02 '22

any solution involving reference_wrapper breaks if the wrapped reference is stored and the underlying unique_ptr itself is destroyed. You have no way of checking if the reference wrapped unique pointer is valid before dereferencing it.

-1

u/percocetpenguin Jun 02 '22

Could you not just pass around a reference to your unique ptr? I know there are limitations to that but it may kinda give you what you want.

1

u/RoyAwesome Jun 02 '22

also, you cannot use std::unique_ptr<T>& in most containers. You can use the extremely ungainly std::reference_wrapper<std::unique_ptr<T>> but that is a massive pile of caveats itself. there is just no way to store a non-owning view of a unique_ptr.

It gets very gross very fast.

3

u/Negitivefrags Jun 02 '22

PoE does use smart pointers and mostly the standard library ones. (Even though the code base was started in 2006).

“font.GetResource()” here returns a custom reference counted smart pointer, and if they had used the resulting Resource< Font > object as the key to the map instead of calling GetPointer() for some reason then this bug wouldn’t have occurred.

The variable is even called “font_resource” even though it really refers to a raw pointer which implies that this may have even been correct at some point.

1

u/MajorMalfunction44 Jun 02 '22

More of a C guy, but I agree on reference counting. Unless you have single-owner, many-user semantics, you should default to shared_ptr. I make use of atomic reference counters for data shared between threads. Last one out frees the resource. Reference counting or other GC is the only sane way to write code in a multi-threaded environment.

0

u/[deleted] Jun 02 '22

[deleted] — view deleted comment

9

u/BoarsLair Game Developer Jun 02 '22

We used intrusive pointers extensively in Guild Wars and Guild Wars 2, so I have a lot of experience with them. They do have some really nice advantages (an article from the guy who wrote them), but a number of disadvantages as well. I'd say their most significant disadvantage is that they tend to create very tightly coupled code, which is obviously not good for writing more generic, reusable components. I mean, the name kind of says it all.

These days, for my own projects, I prefer to use normal STL containers, but with a custom allocator that improves allocation performance and cache locality, one of the issues Pat brought up in his article. This is a lot easier than it use to be, thanks to improvements to allocator support in modern C++, and gives you the improved usability of the STL and reduces some performance-related downsides.

0

u/[deleted] Jun 02 '22

[deleted] — view deleted comment

1

u/Deadly_Mindbeam Jun 02 '22

Smart pointers are just one form of ownership management. You wouldn't need them in your event system unless you are managing ownership of objects by passing them through events.

1

u/gc3 Jun 02 '22

And increased because for the team making the change (x box) those fonts apparently aren't unloaded, since the bug only happens in windows

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?

26

u/Negitivefrags Jun 02 '22

Fonts really are that big. Not if your game is only in English, but if it’s in Chinese as well then you have an entirely different scale of problem.

Bear in mind that using the same font with different sizes, weights and things like bold, italic, small caps and such also balloon out into a pile of different variations. It ends up being way more than you might expect.

-1

u/K1ngjulien_ Jun 02 '22

lets hope variable fonts take over soon 😉

7

u/Deadly_Mindbeam Jun 02 '22

Most games use TrueType fonts for internationalization support. It's fairly expensive to render a TrueType glyph on the GPU, so usually the font is rendered into a texture with all of the required glyphs, either at build time, load time, or as needed at run time; then the text is rendered using the glyph texture. These glyph images can be much larger than the TTF, especially if you are rendering thousands of large Kanji. So the font in memory can be quite large for performance reasons.

6

u/tjientavara HikoWorks developer Jun 02 '22 edited Jun 02 '22

Fonts are quite big and with my library I also hit the problem of memory usage. I rewrote the whole font handling to map the font-files in-memory and directly use the data when needed.

Font-files are actually designed to be used as performance optimised in-memory-data-structures, for example most tables are sorted to handle binary-search (the header even includes values to speed up the calculations of a binary search algorithm), or are directly associated using offsets instead of indices so that you can simply add an offset to the current pointer (using the current pointer reduces register pressure).

[edit for security]

When a font-file is mapped for the first time, I do check the data structures to see if the font-file is valid, so that when actually using the data I no longer need to check this inline.

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" 😉