2 ms·
The author's "internal_ptr" template seems exactly equivalent to Objective-C (and I assume Swift)'s weak references. And that's great! Automatic reference coun
by iainmerrick 10y ago
The author's "internal_ptr" template seems exactly equivalent to Objective-C (and I assume Swift)'s weak references.
And that's great! Automatic reference counting is my single favourite thing about Obj-C. Once you get the hang of using weak refs for parent objects, ARC is very easy to use, with consistent speed, low memory usage, and immediate deallocation of dead objects.
I'd go as far as to say that Objective-C is a big reason why iOS has historically been faster and smoother than Android. It's not the world's fastest language, but there are no big GC pauses and it doesn't waste a lot of memory.
So I bet this is a good memory management style for C++ too.
- putzdown 10y agoNo, Obj-C/Swift use something more akin to C++'s `shared_ptr` and `weak_ptr`, as evidenced by the fact that cyclic references may result in memory leaks in each system. This is precisely the problem that `internal_ptr` seeks to resolve.
- iainmerrick 10y agoAhh, rereading more carefully, I think I see. You could have a chain of objects connected by internal pointers, and they'd all be kept alive as long as the first object is strongly referenced, but that wouldn't work for weak pointers.