FAQ Database Discussion Community


What is the idiomatic way to write a linked list with a tail pointer?

linked-list,rust,reference-counting
As a learning project for Rust, I have a very simple (working, if incomplete) implementation of a singly linked list. The declaration of the structs looks like this: type NodePtr<T> = Option<Box<Node<T>>>; struct Node<T> { data: T, next: NodePtr<T>, } pub struct LinkedList<T> { head: NodePtr<T>, } Implementing size and...

Python sys.getrefcount() shows unexpected 4th reference

python,reference-counting
I'm looking for an explanation about why I have an unexpected reference count. Yes, I already know sys.getrefcount() will increment the expected count by 1. That is not what's happening below. I expect the function test(a) to show 3 not 4. Where is the 4th reference coming from? In [2]:...

Objective-C reference count when pointer set to nil (without ARC)

objective-c,xcode,reference-counting
I'm trying to understand how reference counting works, so I disabled ARC and wrote a simple class: (Foo.h is not pasted as it is unmodified) Foo.m @implementation Foo - (instancetype)init { NSLog(@"Init object"); return [super init]; } - (void)dealloc { NSLog(@"Dealloc object"); [super dealloc]; } @end Main.m #import <Foundation/Foundation.h> #import...

C++ memory management when passing shared_ptr to lambda

c++,lambda,shared-ptr,pass-by-value,reference-counting
Consider the following C++ code: void f(std::function<void()> func) { func(); } void g(std::shared_ptr<MyObject> myObjPtr) { myObjPtr->someMethod(); } void h(std::shared_ptr<MyObject> myObjPtr) { f([=](){ g(myObjPtr); }); } Are there any memory leaks? My understanding is myObjPtr is copied into the lamba and has its reference count incremented. Then it's copied into g()...

Migrating to Unified API and new reference counting

monotouch,xamarin,reference-counting
I had a Xamarin.iOS Classic project and migrated it to Unified API. I have a view controller in my code (simplified version): public class TestViewController : UIViewController { private WeakReference<UIView> _viewRef; public override void ViewDidLoad() { base.ViewDidLoad(); _viewRef = new WeakReference<UIView>(View); StartTimer(); } private async void StartTimer() { await Task.Delay(1000);...

Is fetch_sub really atomic?

c++,multithreading,reference-counting,stdatomic
I have the following code (written in C++): Code in StringRef class: inline void retain() const { m_refCount.fetch_add(1, std::memory_order_relaxed); } inline void release() const { if(m_refCount.fetch_sub(1, std::memory_order_release) == 1){ std::atomic_thread_fence(std::memory_order_acquire); deleteFromParent(); } } Code in InternedString: public: inline InternedString(){ m_ref = nullptr; } inline InternedString(const InternedString& other){ m_ref = other.m_ref;...