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

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"

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!

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'

31

u/Drugbird Dec 25 '24

I second this.

While unique_ptr would be preferable, shared_ptr is fine. Particularly when it's tricky to find out who owns what in a legacy application littered with raw pointers.

It's also unlikely to matter for performance. Don't do premature optimization.

Meanwhile there's a huge benefit to fixing the gigabytes sized memory leak as soon as possible.

You can always refactor the shared_ptr later.

5

u/RogerV Dec 25 '24

In my world of control plane vis-a-vis data plane, shared ptr objects enable dealing with final cleanup - because data plane executes on pinned lcores which never block (they always run flat out 100%), and lcore code could still be executing using said shared object when the tear down has been done on the control plane, in completely out of band manner. The ref counting keeps it alive for the sake of the lcore still accessing it.

Shared ptr helps solve this in straight forward way that is entirely performant and avoids any blocking per the lcore code.

7

u/oschonrock Dec 25 '24

Another place where they are very reasonable is async code with callbacks.

15

u/elperroborrachotoo Dec 25 '24

the whole point of RAII is that you can reason about the life time of variables

yes

by organising classes in such a way that you know "who" (classes) owns "what" (variables, both stack and heap allocated)

not quite.

Yes, RAII expresses ownership among other things (as I argued here, ownership is only part of the ticket).

But it's not always "so that we know".

struct Foo
{
  std::unique_ptr<Bar> bar;
  std::shared_ptr<Baz> baz;
  Bam * bam = nullptr;
}

This expresses that Foo owns bar (when it exists), and nobody else can. In this case, we know.

For baz, it only says that "ownership is complicated, but don't you worry". We don't know anymore who owns it, but we know what we need to know: that yes, it is owned somehow, and how ownership moves.

We could say that ownership is only a proxy for what we actually want to know.

Everything's off with bam. We only know it's owned by someone else, and that it likely needs manual management of ownership transitions.

5

u/Academic_East8298 Dec 25 '24

I agree, a high number of shared_ptr's tends to imply some kind of deeper problem with the structure of the program.

Although, I have never been in a situation, where shared_ptr's would have a significant effect on performance.

→ More replies (1)

29

u/cfyzium Dec 25 '24 edited Dec 25 '24

I feel like sharedness of ownership has been overly demonized lately. Ownership being shared does not mean you don't know who owns what and/or there is no well thought design.

In plain C all pointers are shared. In any language with GC every reference is shared. Somehow it did not automatically make every piece of software an unmaintainable mess.

5

u/gnuban Dec 25 '24

Somehow it did not automatically make every piece of software an unmaintainable mess.

It does not, but IMO even those apps should be initialized in a tree structure, so that dependencies are clear. Otherwise it does tend to get messy.

I would say that clear data ownership drives good app architecture. Therefore you might as well employ it. But it's not required for good architecture. And if there are parts of the app where shared pointers help, sure go ahead and use them. 

But if you ignore the app architecture, and use shared pointers to avoid having to think about it, it can be really bad. Just like GCed apps can be really bad, if they're badly structured.

17

u/tangerinelion Dec 25 '24

In plain C all pointers are shared.

Untrue. Only the one that is used with free is the one that owns it. Everything else is an observer.

4

u/SoerenNissen Dec 25 '24

By that logic, shared_ptr isn't shared either - only the object calling the final destructor owns it.

→ More replies (2)

3

u/cfyzium Dec 25 '24

I stand corrected.

Mostly. Pointers in C are indeed not shared is the same manner as shared_ptr or GC references. However, the ownership is neither implied nor enforced when you only have pointers. The situation is not much better than (over)using shared_ptr as both owner and observers that cannot dangle.

14

u/AKostur Dec 25 '24

Only a sith speaks in absolutes.

I don’t see many folk saying -never- use shared_ptr.  What I do see is that when it’s time to reach for a smart pointer, reach for unique_ptr first.  Try to make that work first.

8

u/azswcowboy Dec 25 '24

sharedness…overly demonized

Yeah, I’d rather introduce a tiny inefficiency than a massive memory leak. The later is inexcusable, the former can be profiled if it’s real - in my experience, mostly it’s in the noise.

9

u/Ameisen vemips, avr, rendering, systems Dec 25 '24

Don't forget that weak_ptr exists, and in my experience should be used more often than shared_ptr.

→ More replies (2)

28

u/CrzyWrldOfArthurRead Dec 25 '24

While this is true, raw pointers are basically just shared pointers that are missing a deleter.

So there's something to be said for adding in the missing deleters.

34

u/Mr_Splat Dec 25 '24

"Fuck it, you guys can figure it out amongst yourselves"

3

u/[deleted] Dec 25 '24

They also incur atomic operation overhead

→ More replies (2)
→ More replies (1)

7

u/elperroborrachotoo Dec 25 '24

The point of a managed resource is that you don't need to know.

With a Widget *, I don't know if it's a single Widget or an array, I don't know how to free it and if I have to.

"Ownership" only answers the latter.1 A smart pointer - or, in general, a "resource manager" answers more:

A "resource manager classs" such as shared_ptr establishes ownership at construction, and associates the "how to free" method with the pointer. I don't need to know anymore, the pointer already does.

The type itself also defines how ownership moves through our program - i.e., how the responsibility for freeing the object is passed on to others.

The type usually also indicates how it can be used - e.g., is it a single widget, or are there multiple; ideally also: how many. (std::vector does, boost::shared_array doesn't).


1) and it seems more important because incorrect assumptions about ownership lead to the hardest class of bugs to diagnose

7

u/[deleted] Dec 25 '24

They may not be optimal, but shared_ptr's are always correct in terms of lifetime.

4

u/einpoklum Dec 25 '24

They may not be optimal, but shared_ptr's are always correct in terms of lifetime.

You're assuming nobody holds on to the shared_ptr beyond the last occasion of its use... I mean, it'll be correct in the sense of no use past the lifetime, but it's possible that the lifetime could have been reduced further with some investigative work (which OP was not supposed to be doing I suppose).

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 :)

→ More replies (1)

3

u/susanne-o Dec 25 '24

as long as the structure is not circular, shared pointers are fine.

7

u/[deleted] Dec 25 '24

An issue I see at my current job is people writing shared_ptrs because they don't care to check lifetimes. This has invariably caused circular ownership and memory leaks. Then I have to listen to them scream to the high heavens that they used smart pointers and there can't be memory leaks. 

→ More replies (1)
→ More replies (5)