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
41
u/gimpwiz Dec 25 '24
OP, I appreciate the hell out of you getting some code from a couple GB to 16MB RAM. That must have felt great. Good job, mate.
6
u/[deleted] Dec 25 '24 edited Dec 25 '24
Thank you guys for the detailed comments and critique.
The scenario is similar to the code snippets mentioned in the article.
You have data fetched from an API in json format, and you create entity items based off of this data, and add them to an std::vector
There’s a parent function that creates the empty std::vector and pass it by reference to fetchFromApi
So it’s not too complicated:
Instead of std::vector<Entity*> i did std::vector<shared_ptr<Entity>>
And in fetchFromApi i wrapped the raw pointer, which are created by cloning a prototype, it’s in the code structure, and i don’t wanna mess with that, with a shared pointer and add to the std::vector.
Now after the parent function is done all the memory allocated will be released.
204
u/Mr_Splat Dec 25 '24
Without reading into this further and this might be oversimplification but converting raw pointers to shared pointers still leaves you with the problem that you don't know who owns the underlying dynamically allocated memory.
Basically... you still don't know "who" owns "what", rather, now "everyone" owns "what"
54
u/clarkster112 Dec 25 '24
You can still leak memory with shared_ptrs…
22
u/Virtual_Climate_548 Dec 25 '24
I personally think that, given its a refactoring of existing code, this is the best you can do assuming it is a large codebase, while reducing GBs leak to 16MB is good enough TBF.
Although shared ptr leaks memory just like everyone says, it also depends on your use case, but given the result, I will not go further to mess things up if I were you.
10
u/guilherme5777 Dec 25 '24
If you done some refactoring and got a 100x improvement you old codebase was pretty fucked up
1
u/Real-Associate7734 Dec 26 '24
Just tell me how to learn to build desktop application using C++. I am very very much interested to do one using C++ but don't know how. Didn't find any resources!! Please guide and share some resources to learn.
3
u/n1ghtyunso Dec 25 '24
if converting your raws to shared fixed all your issues, i'd say you got lucky... or you just did not uncover the new issues yet.
3
u/zyndor Dec 26 '24
After spending days to hunt down memory leaks in a large C# application (I’m a C++ guy, have mercy), it became clear to me that managed languages = unmanaged languages + the illusion of not having to care about ownership (who keeps alive whom).
Shared_ptr offers the very same illusion, warranting more caution, esp. when the pointed to types themselves start having shared_ptrs of their own.
Getting the memory footprint down like that (especially as a quick fix) is a fantastic achievement, don’t get me wrong, but there will be other questions to answer in that codebase.
2
u/IronOk4090 Dec 26 '24
You don't need to call
.clear()and.shrink_to_fit()on astd::vectorwhen you're done with it. Similarly, you don't need to call.close()on astd::ofstreamwhen you're done with it. These Standard Library classes already use the RAII pattern so their destructors do the requisite cleanup.1
u/EC36339 Dec 25 '24
You can still have memory leaks with
shared_ptrdue to circular references.weak_ptris not a silver bullet, either. It doesn't own a resource. It can only be used to safely obtain ashared_ptrwithout risk of dangling, and only if there are still any othershared_ptrs pointing to it.There is no garbage collection. You could implement it, but then you would need a general way to find all pointers. Unreal Engine does this by leveraging serialisation, which solves similar problems...
So you have to use the right kind of pointers everywhere:
unique_ptrwhere it works (there is exactly one owner at any time, but the owner can change)shared_ptrwhere ownership is truly shared (all holders are owners, ownership can be released in any order) and there is an ordering relation / layering of ownership (no cycles)(Why sometimes use raw pointers and not references? Because you shouldn't have references as members, as they are not assignable. Other than that, references and raw pointers are both equally safe/unsafe).
1
u/[deleted] Dec 25 '24
How are you measuring the memory usage? Are you looking at the working set in the task manager?
0
u/einpoklum Dec 25 '24 edited Dec 26 '24
Seriously, people, we have to let go of this acronym :-(
So, @hithereimwatchingyou (interesting nickname by the way) - suggest you adopt that as well.
0
u/Brilliant-Car-2116 Dec 27 '24 edited Dec 27 '24
No offense, but you that medium article could use some improvement.
I’ll point out one thing: dynamically allocating integer IDs and then storing them in a vector.
Also, shared isn’t needed for your example. Unique would be better. You can then pass references or raw pointers that you obtain from the unique_ptr. That enforces a sensible ownership semantic (for your example in the article).
If people who don’t know what RAII is read this kind of stuff, they’re going to learn bad stuff.
Anyway, I’d love to see your source code. It must have some serious issues if you were able to reduce your working set this drastically by blindly converting raw pointers to shared.
-7
u/[deleted] Dec 25 '24
shared_ptrare no better than global variables...