2 ms·
"You basically get full memory management without garbage collection" No, you don't. Reference counting is not equivalent to garbage collection. Edit: Garbage
by fsloth 8y ago
"You basically get full memory management without garbage collection"
No, you don't. Reference counting is not equivalent to garbage collection.
Edit: Garbage collection has no problem collecting circular references if they are out of scope. Not so much for reference counted pointers. You will start leaking memory accidentally if you are not carefull - i.e. reference counting is not "automatic full memory management" - it will delete stuff once it's reference count goes to zero but it you will have to be careful not to come up with circular references that will leak.
- Jare 8y agoIf you can manage to not use shared_ptr (and, as the author says, you should try to) then you are not reference counting and you don't have circular references. As with Rust, the very few places where you may need such constructs can be carefully isolated, the other 99% of your code is "basically" safe from dangling pointers and memory leaks.
- bnolsen 8y agoDon't write circular references. That's shit design.
- hedora 8y agoBut you don’t get full memory management with garbage collection either. Take MVC GUI programming in Java, which I mention because the thread is about Java, and considerable effort was made by the core Java team at Sun to make this work well. If you use swing or awt and you call “longLivedThing.addEventListener(shortLivedThing)“ then you’ve written a memory leak. The two options: (1) create a directed a cyclic graph encoding desired object lifetimes, and make sure references obey it, or (2) use weak references to manually break the cycles. (1) is much easier in C++ because you can use rich reference types to get the compiler to automatically audit the lion’s share of your codebase. I don’t know of anyone that actually does (2) in Java because it makes it extremely difficult to reason about the actual runtime behavior of the program.