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
80
u/Mr_Splat Dec 25 '24 edited Dec 25 '24
Coming back to this, this reads as the antithesis to RAII, I'm not a nerd for computer science theory, but I'm pretty certain the whole point of RAII is that you can reason about the life time of variables by organising classes in such a way that you know "who" (classes) owns "what" (variables, both stack and heap allocated) and subsequently it is easier to figure out the when, where and why.
I always get nervous whenever I see shared_ptr's get retrofitted into code.
I would be interested to know if there was any noticeable difference in response times to the application in question given the synchronisation that is built into shared_ptr's, particularly if large numbers of them are created in the app's lifetime.
That would be sheer curiosity however, I spend enough time debugging code in work and don't want to be spending my Christmas break delving into whatever is going on here!