Hacker News new | ask | show | jobs
by ufmace 2535 days ago
> That's false, isn't it? C# is a memory safe language, period. It relies on GC for that.

The original post's whole sentence there is basically nonsense. They're different enough that it's hard to call either one more advanced or having better memory protection at all.

Using just plain C#, it has better memory protection, since the GC and runtime make it impossible to leak, double-free, or access out-of-bounds. But there's a higher cost for that, and the system for managing non-memory resources is not as reliable - you can leak file handles, network handles, etc more easily. Not up on the latest in Rust, but I think it's possible to leak memory if you are sufficiently clever and doing really weird stuff. But the standard ownership model is great at making it really hard to leak or mismanage any resource, not just memory.

It's hard to call either one more advanced either. .NET has a massive std lib and C# has been getting some cool new features. It's still a runtime language though, with the limitations that come from that. Rust's lifetime and ownership system is pretty advanced, but wouldn't make sense for a runtime language.

2 comments

> the GC and runtime make it impossible to leak, double-free, or access out-of-bounds

This is false.

Neither C# nor Rust protect from memory leaks. There are actually some gotchas in C# that can cause memory leaks - for example, you have to be very careful about events. Memory leaks are not memory unsafe, though.

On the topic of actual memory safety - C# has unsafe blocks just like Rust does. Safe Rust is just as safe from memory safety problems as safe C#, even more so, because C# doesn't require unsafe for FFI, where all bets are off. And unsafe C# is just as unsafe as unsafe Rust can be.

Yes, leaks are possible in all languages.

In Rust you can leak due to reference cycles for example: https://doc.rust-lang.org/book/ch15-06-reference-cycles.html

C# will clean up reference cycles, but an unintended reference can cause leaks (if you forget to remove it).

In both languages such leaks are not good, but at least they aren't memory safety issues.

> you have to be very careful about events

The only gotcha that can cause memory leaks in C# is a GC root, i.e. referencing something on a stack, that is static or that is pinned. Events/delegates are not special in any way, they are simply a reference footgun because it's non-obvious that they contain a reference to the object that contains the handler.

> I think it's possible to leak memory if you are sufficiently clever and doing really weird stuff

Note that Rust provides an easy way to intentionally leak memory, unlike many other languages: https://doc.rust-lang.org/std/mem/fn.forget.html

`mem::forget()` only prevents destructors from running, which leaks data managed by the destructor as a side effect, but it gives no guarantees about not freeing the object passed to it (e.g. if you call it on a stack-allocated object, it can't leak it).

`Box::leak()` leaks memory.