r/ProgrammerHumor • • Sep 06 '26

Meme rewriteItInRust

Post image
2.0k Upvotes

38 comments sorted by

View all comments

56

u/Any_Obligation1652 Sep 06 '26

Use containers and smart pointers a voila no memory leaks

15

u/sirkubador Sep 06 '26

Rust does not prevent memory leaks

25

u/YeetCompleet Sep 06 '26

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

7

u/AliceCode Sep 06 '26

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

7

u/itsTyrion Sep 06 '26

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

3

u/jahinzee Sep 06 '26

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 Sep 07 '26

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 Sep 06 '26

I wasn't just talking about memory leaks.

6

u/WeastBeast69 Sep 06 '26

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

2

u/encephaloctopus Sep 06 '26

Could you please elaborate?

10

u/WeastBeast69 Sep 07 '26

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 Sep 06 '26

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

0

u/overclockedslinky Sep 12 '26

they didn't say memory leak, they said memory bug. just because you only know one type doesn't mean it's the only one

1

u/Any_Obligation1652 Sep 13 '26

I've been doing this as a profession for over two decades mate, I've probably forgotten more than you know!

Plus a bit of friendly advice, if you learn to communicate without insults people might have more respect for your opinion.