Questions tagged [automatic-ref-counting]

Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory management of Objective-C and Swift objects.

Automatic Reference Counting (ARC) is a technology introduced in LLVM 3.0 (which ships with Xcode 4.2) for managing memory in Objective-C. Swift also uses ARC. This method removes the need for much of the code previously required for manual memory management (MRR) of objects in Objective-C (such as manually calling release) and introduces new memory management qualifiers (namely strong and weak). ARC is a compile-time feature that transparently incorporates the appropriate reference-counting memory management calls into the final application. Like manual reference counting, there is no run-time garbage collection.

References:

See Also

  • For the Arc programming language, see arc-lisp.
3809 questions
1371
votes
18 answers

How can I disable ARC for a single file in a project?

I am using ARC successfully in my project. However, I have encountered a few files (e.g., in unit tests and mock objects) where the rules of ARC are a little more fragile right now. I recall hearing that there was a way to disable ARC on a per-file…
casademora
  • 67,775
  • 17
  • 69
  • 78
1301
votes
19 answers

performSelector may cause a leak because its selector is unknown

I'm getting the following warning by the ARC compiler: "performSelector may cause a leak because its selector is unknown". Here's what I'm doing: [_controller performSelector:NSSelectorFromString(@"someMethod")]; Why do I get this warning? I…
Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
574
votes
11 answers

Should IBOutlets be strong or weak under ARC?

I am developing exclusively for iOS 5 using ARC. Should IBOutlets to UIViews (and subclasses) be strong or weak? The following: @property (nonatomic, weak) IBOutlet UIButton *button; Would get rid of all of this: - (void)viewDidUnload { // ... …
505
votes
11 answers

Shall we always use [unowned self] inside closure in Swift

In WWDC 2014 session 403 Intermediate Swift and transcript, there was the following slide The speaker said in that case, if we don't use [unowned self] there, it will be a memory leak. Does it mean we should always use [unowned self] inside…
Jake Lin
  • 11,146
  • 6
  • 29
  • 40
380
votes
8 answers

Objective-C ARC: strong vs retain and weak vs assign

There are two new memory management attributes for properties introduced by ARC, strong and weak. Apart from copy, which is obviously something completely different, are there any differences between strong vs retain and weak vs assign? From my…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
330
votes
8 answers

Can I use Objective-C blocks as properties?

Is it possible to have blocks as properties using the standard property syntax? Are there any changes for ARC?
gurghet
  • 7,591
  • 4
  • 36
  • 63
312
votes
2 answers

Why does Apple recommend to use dispatch_once for implementing the singleton pattern under ARC?

What's the exact reason for using dispatch_once in the shared instance accessor of a singleton under ARC? + (MyClass *)sharedInstance { // Static local predicate must be initialized to 0 static MyClass *sharedInstance = nil; static…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
293
votes
4 answers

Objective-C declared @property attributes (nonatomic, copy, strong, weak)

Can someone explain to me in detail when I must use each attribute: nonatomic, copy, strong, weak, and so on, for a declared property, and explain what each does? Some sort of example would be great also. I am using ARC.
Gaurav_soni
  • 6,064
  • 8
  • 32
  • 49
290
votes
10 answers

Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

I'm currently using the iOS 5 SDK trying to develop my app. I'm trying to make an NSString a property, and then to synthesize it in the .m file (I have done this before with no issues). Now, I came across this: "Semantic Issue: Property's…
Noam
  • 3,100
  • 3
  • 16
  • 19
284
votes
8 answers

When converting a project to use ARC what does "switch case is in protected scope" mean?

When converting a project to use ARC what does "switch case is in protected scope" mean? I am converting a project to use ARC, using Xcode 4 Edit -> Refactor -> Convert to Objective-C ARC... One of the errors I get is "switch case is in protected…
Ali
  • 18,665
  • 21
  • 103
  • 138
268
votes
6 answers

Always pass weak reference of self into block in ARC?

I am a little confused about block usage in Objective-C. I currently use ARC and I have quite a lot of blocks in my app, currently always referring to self instead of its weak reference. May that be the cause of these blocks retaining self and…
the_critic
  • 12,720
  • 19
  • 67
  • 115
264
votes
7 answers

What is the difference between a weak reference and an unowned reference?

Swift has: Strong References Weak References Unowned References How is an unowned reference different from a weak reference? When is it safe to use an unowned reference? Are unowned references a security risk like dangling pointers in C/C++?
238
votes
5 answers

What kind of leaks does automatic reference counting in Objective-C not prevent or minimize?

In the Mac and iOS platforms, memory leaks are often caused by unreleased pointers. Traditionally, it has always been of utmost importance to check your allocs, copies and retains to make sure each has a corresponding release message. The toolchain…
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
229
votes
18 answers

How do I declare an array of weak references in Swift?

I'd like to store an array of weak references in Swift. The array itself should not be a weak reference - its elements should be. I think Cocoa NSPointerArray offers a non-typesafe version of this.
Bill
  • 44,502
  • 24
  • 122
  • 213
221
votes
8 answers

How do I avoid capturing self in blocks when implementing an API?

I have a working app and I'm working on converting it to ARC in Xcode 4.2. One of the pre-check warnings involves capturing self strongly in a block leading to a retain cycle. I've made a simple code sample to illustrate the issue. I believe I…
XJones
  • 21,959
  • 10
  • 67
  • 82
1
2 3
99 100