Questions tagged [reference-counting]

Reference counting is a mechanism whereby a computer language keeps track of when you create and then no longer need an object or data structure, in order to free unneeded memory. Traditionally this would have been done manually by the programmer.

461 questions
67
votes
4 answers

How to force deletion of a python object?

I am curious about the details of __del__ in python, when and why it should be used and what it shouldn't be used for. I've learned the hard way that it is not really like what one would naively expected from a destructor, in that it is not the…
wim
  • 338,267
  • 99
  • 616
  • 750
65
votes
10 answers

Why no Reference Counting + Garbage Collection in C#?

I come from a C++ background and I've been working with C# for about a year. Like many others I'm flummoxed as to why deterministic resource management is not built-in to the language. Instead of deterministic destructors we have the dispose…
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
51
votes
3 answers

Should I refer to self.property in the init method with ARC?

A quick question. if I have a property and an ivar declared with the same name: in the .h file: (Reminder*)reminder; @property(nonatomic,strong)(Reminder*)reminder; in the .m file, should I use the ivar or the property in the init method if I'm…
48
votes
6 answers

Why don't purely functional languages use reference counting?

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the…
42
votes
4 answers

What are the advantages and disadvantages of using ARC?

What are the advantages and disadvantages of using the new automatic reference counting (ARC) memory management style in an iOS project? Can you choose not to use ARC when developing with the iOS 5.0 SDK? Do you recommend ARC or manual reference…
user141302
42
votes
2 answers

When to use Rc vs Box?

I have the following code which uses both Rc and Box; what is the difference between those? Which one is better? use std::rc::Rc; fn main() { let a = Box::new(1); let a1 = &a; let a2 = &a; let b = Rc::new(1); let b1 =…
o0omycomputero0o
  • 3,316
  • 4
  • 31
  • 45
39
votes
2 answers

Calling -retainCount Considered Harmful

Or, Why I Didn't Use retainCount On My Summer Vacation This post is intended to solicit detailed write-ups about the whys and wherefores of that infamous method, retainCount, in order to consolidate the relevant information floating around SO.* The…
jscs
  • 63,694
  • 13
  • 151
  • 195
35
votes
4 answers

What is a reference cycle in python?

I have looked in the official documentation for python, but i cannot seem to find what a reference cycle is. Could anyone please clarify what it is for me, as i am trying to understand the GC module. Thank you in advance for your replies.
IT Ninja
  • 6,174
  • 10
  • 42
  • 65
30
votes
2 answers

Why should Py_INCREF(Py_None) be required before returning Py_None in C?

Why should Py_INCREF(Py_None) be required before returning Py_None in C as follows? Py_INCREF(Py_None); return Py_None; If Py_INCREF(Py_None) is omitted, what will happen?
Johnny Lim
  • 5,623
  • 8
  • 38
  • 53
28
votes
4 answers

Why isn’t my weak reference cleared right after the strong ones are gone?

I am a little bit stubborn, but I want to understand weak and strong references well, so that's why I'm asking you once again. Consider this: __weak NSString* mySecondPointer = myText; NSLog(@"myText: %@", myText); The result is myText: (null)…
28
votes
2 answers

WeakReference implementation in .NET

I understand and appreciate the usefulness of the System.WeakReference class in the .NET framework, but am curious as to the implementation details. How is WeakReference implemented in .NET? MSDN discusses the usage of WeakReference in detail, but…
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
27
votes
1 answer

How do I share a mutable object between threads using Arc?

I'm trying to share a mutable object between threads in Rust using Arc, but I get this error: error[E0596]: cannot borrow data in a `&` reference as mutable --> src/main.rs:11:13 | 11 | shared_stats_clone.add_stats(); | …
Razican
  • 697
  • 2
  • 10
  • 16
24
votes
13 answers

How to detect cycles when using shared_ptr

shared_ptr is a reference counting smart pointer in the Boost library. The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in C++. Please no suggestions like: "don't make…
Unknown
  • 45,913
  • 27
  • 138
  • 182
24
votes
2 answers

Is there an operation for Rc or Arc which clones the underlying value and returns it to the caller?

I'm looking for something roughly like this take, but atomic: impl for Arc { fn take(mut self) -> T { Arc::make_mut(&mut self); Arc::try_unwrap(self).unwrap() } } In other words, I want Arc::make_mut which…
Araz Abishov
  • 1,661
  • 3
  • 15
  • 26
24
votes
5 answers

Atomic Reference Counting

I'm trying to understand exactly how thread-safe, atomic reference counting works, for example as with std::shared_ptr. I mean, the basic concept is simple, but I'm really confused about how the decref plus delete avoids race conditions. This…
Siler
  • 8,976
  • 11
  • 64
  • 124
1
2 3
30 31