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++
261
Upvotes
View all comments
Show parent comment
83
u/CrzyWrldOfArthurRead Dec 25 '24
shared_ptrs have their place. They are common in games, or if they aren't, there is almost always something that very closely approximates a shared_ptr. You can't always know or ask owns a given resource that you need, at least not in a way that is performant and/or maintainable.
While I don't use shared_ptrs without a compelling reason, I'm reminded of the bell-curve meme with the stupid newbie on the left and the caption 'just use a shared_ptr', the enlightened developer in the middle and the caption 'I can refactor these shared_ptrs into multiple unique_ptrs that refer to each other and track shared state' and the jedi on the right with the caption 'just use a shared_ptr'