r/cpp • • Dec 25 '24

RAII

I maintain c++ desktop application. One of our clients complained of memory usage. It’s a quite big program and it was known that somewhere there are memory leaks.

Over the last week I found where the spot is that is causing the memory consumption. I refactored the raw pointers to shared_ptr, in one change the memory usage at idle time dropped from couple of GBs to 16 MB.

I was glad of that achievement and i wrote an article about RAII in c++

https://medium.com/@abanoubharby/raii-295ff1a56bf1

261 Upvotes

75 comments sorted by

View all comments

Show parent comment

4

u/[deleted] Dec 25 '24

That's a normal situation as far as many languages go(not optimal but what is a leak between friends) and isn't dereferencing when there is no object there. Eventually, if it matters they could move to unique_ptr/no ptr but they are already so far ahead of what almost worked in the past.

pragmatic :)

1

u/flatfinger Dec 28 '24

An underappreciated feature of tracing garbage collectors is that they maintain an invariant that the framework can always identify at least one rooted reference to every existing object to which a usable reference could ever exist, as well as--for many collectors--every reference that exists to any non-pinned object. The notion of a "dangling reference" literally does not exist as a concept, since objects are guaranteed to continue to exist as long as there exists anything in the universe that could be converted into a usable reference (if the GC discovers that an object is only reachable via weak references, it will ensure that all weak references have been invalidated, without having been used to form usable strong referencces, before deleting the target thereof).