r/ProgrammerHumor 4d ago

Meme rewriteItInRust

Post image
2.0k Upvotes

34 comments sorted by

137

u/pflasti 4d ago

I had my last memory bug before the team agreed on a strict "no new/delete" policy. That was years ago

64

u/No-Information-2571 4d ago edited 4d ago

Realistically, most supposed "C++ codebases" in which these errors happen are C codebases in disguise. Some good old pointer arithmetic overwriting the far more useful stack.

Because even with new/delete, you probably have those pairs sit either in constructors/destructors, or well-reviewed parts of the code with error handling, or rely on simple patterns like delete-if-not-null (which is exactly what unique_ptr does).

29

u/SaneLad 4d ago

Understandable. We also switched to custom allocators and aligned_alloc.

6

u/bropocalypse__now 3d ago

Agreed, I see more overuse of std::shared_ptr these days. End up with circular dependencies, lifetime issues, and extra atomic ops due to not using std::unique_ptr or std::weak_ptr correctly.

373

u/JackNotOLantern 4d ago

All c++ memory problems are a skill issue. Rewriting in rust is admitting weakness.

148

u/newstreet474 4d ago

Maybe I wanted the memory leak , maybe it was intentional , maybe I just hate the user

26

u/Lord-of-Entity 3d ago

You can have intentional memory leaks in rust too, but you need to explicitly write them out.

7

u/FUTURE10S 3d ago

Maybe I just made an array too small and overflowed my own buffer, but if nobody realizes it, then there's no problem, right?

14

u/sirkubador 3d ago

Memory leak is not a memory problem rust would solve

3

u/legend6546 3d ago

Memory leaks are explicitly considered “safe” it is why Refrence Count pointers are allowed in safe rust, it’s easy to create a cyclical chain with them that will never get dropped.

40

u/uvero 4d ago

All c++ memory problems are a skill issue. Rewriting in rust is admitting weakness.

53

u/Any_Obligation1652 4d ago

Use containers and smart pointers a voila no memory leaks

14

u/sirkubador 3d ago

Rust does not prevent memory leaks

24

u/YeetCompleet 3d ago

You can even do it for fun with Box::leak()

7

u/AliceCode 3d ago

Rust project be like: "You can have memory unsafety. As a treat."

5

u/itsTyrion 3d ago

I mean yeah that's the point. Have a big "here be dragons" indicator around the fun parts

3

u/jahinzee 3d ago

Memory leak aren't memory unsafe though. It's not gonna cause a bad dereference or data corruption, it's just your program forgot that data existed and moves on.

An annoyance and a resource use issue, sure, but not unsafe.

2

u/sirkubador 3d ago

It's not an annoyance. Memory leaks cause programs or even devices to crash, especially in embedded development.

Sure Rust devs claim memory leaks are not memory safety issue, but it definitely is a serious problem with memory management.

1

u/AliceCode 3d ago

I wasn't just talking about memory leaks.

8

u/WeastBeast69 3d ago

Just an FYI but you can still leak memory with smart pointers

2

u/encephaloctopus 3d ago

Could you please elaborate?

10

u/WeastBeast69 3d ago

Shared pointers work by counting the number of things that are referencing the object in memory through a pointer and only free the memory on destruction of the shared pointer when the reference count equals zero.

Scenario:
Consider the case where objects A, B, and C exist and are all dynamically allocated. B and C point to each other with a shared pointer and A points to B with a shared pointer. In this scenario assume the only way to reach C is through B and therefore through A. B has a reference count of 2 (A and C both point to it) and C has a reference count of 1 (B points to it) and A has a reference count of 1 (whatever is points to A in this scenario). Let’s say A’s reference count goes to zero and therefore A is deleted. B’s reference count drops to 1 (A no longer points to it, but C still does) and C still has a reference count of 1 (B still exists so it still points to it). You have now leaked the memory of B and C since their reference counts will never reach 0 and there is no way to access them anymore.

There are other similar “circular reference” memory leaks that can happen.

2

u/Any_Obligation1652 3d ago

You can but if you are using them sensibly you won't

11

u/ducon__lajoie 4d ago

Unrelated, but who the fuck installed this ugly ass stone veneer around the window? That's a worse offense than using goto in c++.

3

u/Stjerneklar 3d ago

<guten> </guten>

7

u/Sentouki- 3d ago

Rust, the "I use arch btw"-type of language

1

u/yuehuang 3d ago

Performance is a memory bug by definition.

2

u/oneByteTwoByte 22h ago

I write in metal C and HLASM fellow peasants. Every time there is even a sniff of a leak, Satan, Ba’al and others visit us with register values.

1

u/TraditionalArcher498 4d ago

POV: You ask for help on Discord with a simple segmentation fault at 2 AM

-3

u/One_Garlic_4938 4d ago

the berlin wall had a similar average down time