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-ref-counting
3,824
questions
1373
votes
18
answers
341k
views
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 ...
1304
votes
19
answers
191k
views
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(@"...
575
votes
11
answers
131k
views
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 ...
510
votes
12
answers
177k
views
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 ...
382
votes
8
answers
209k
views
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 ...
331
votes
8
answers
127k
views
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?
313
votes
2
answers
80k
views
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
...
293
votes
4
answers
164k
views
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 ...
291
votes
10
answers
82k
views
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 ...
284
votes
8
answers
74k
views
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 ...
271
votes
6
answers
127k
views
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 ...
264
votes
8
answers
84k
views
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 ...
239
votes
5
answers
42k
views
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 ...
235
votes
18
answers
65k
views
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.
221
votes
7
answers
93k
views
capturing self strongly in this block is likely to lead to a retain cycle
How can I avoid this warning in xcode. Here is the code snippet:
[player(AVPlayer object) addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
queue:nil usingBlock:^(CMTime time) {
...
221
votes
8
answers
82k
views
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 ...
212
votes
6
answers
61k
views
How does the new automatic reference counting mechanism work?
Can someone briefly explain to me how ARC works? I know it's different from Garbage Collection, but I was just wondering exactly how it worked.
Also, if ARC does what GC does without hindering ...
211
votes
1
answer
73k
views
Custom dealloc and ARC (Objective-C)
In my little iPad app I have a "switch language" function that uses an observer. Every view controller registers itself with my observer during its viewDidLoad:.
- (void)viewDidLoad
{
[super ...
209
votes
8
answers
74k
views
Why is @autoreleasepool still needed with ARC?
For the most part with ARC (Automatic Reference Counting), we don't need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePools anymore, ...
183
votes
4
answers
55k
views
Disable Automatic Reference Counting for Some Files
I have downloaded the iOS 5 SDK and found that ARC is a great feature of the new Apple compiler. For the time being, many third party frameworks don't support ARC. Could I use ARC for my new code and ...
178
votes
10
answers
98k
views
How do I implement an Objective-C singleton that is compatible with ARC?
How do I convert (or create) a singleton class that compiles and behaves correctly when using automatic reference counting (ARC) in Xcode 4.2?
172
votes
3
answers
88k
views
ARC and bridged cast
With ARC, I can no longer cast CGColorRef to id. I learned that I need to do a bridged cast. According clang docs:
A bridged cast is a C-style cast annotated with one of three keywords:
(...
143
votes
7
answers
49k
views
Fix warning "Capturing [an object] strongly in this block is likely to lead to a retain cycle" in ARC-enabled code
In ARC enabled code, how to fix a warning about a potential retain cycle, when using a block-based API?
The warning:
Capturing 'request' strongly in this block is likely to lead to a retain cycle
...
135
votes
5
answers
55k
views
Some questions about Automatic Reference Counting in iOS5 SDK
I'm currently developing an app for iPad. The development started for iOS 4.2 and is now continuing (and I think will be completed) for iOS 4.3.
I just read about ARC in iOS 5, and basically I ...
127
votes
4
answers
30k
views
In which situations do we need to write the __autoreleasing ownership qualifier under ARC?
I'm trying to complete the puzzle.
__strong is the default for all Objective-C retainable object pointers like NSObject, NSString, etc.. It's a strong reference. ARC balances it with a -release at ...
126
votes
2
answers
34k
views
Do I set properties to nil in dealloc when using ARC?
I am trying to learn Automatic Reference Counting in iOS 5. Now the first part of this question should be easy:
Is it correct that I do NOT need to write explicit
release-property statements in my ...
118
votes
1
answer
31k
views
What does "Receiver type 'CALayer' for instance message is a forward declaration" mean here?
I'm porting a block of code from an iOS4 project to iOS5 and I'm having some troubles with ARC. The code generates a PDF from a screen capture.
PDF Generation Code
UIView *captureView;
...
...
117
votes
6
answers
52k
views
Explanation of strong and weak storage in iOS5
I am new to iOS5 development and using objective-c. I have trouble understanding the difference between strong and weak storage. I have read the documentation and other SO questions, but they all ...
113
votes
6
answers
24k
views
To ARC or not to ARC? What are the pros and cons? [closed]
I've yet to use ARC, since the majority of the code in the project I'm working on at the moment was written pre-iOS 5.0.
I was just wondering, does the convenience of not retaining/releasing ...
111
votes
3
answers
29k
views
UIPopovercontroller dealloc reached while popover is still visible
I assure you that I did look for an answer in SO for my question but none of them were helpful. Here I got a simple code that should present a UIImagePickerController within a UIPopoverController:
-(...
109
votes
7
answers
58k
views
iOS 5 Best Practice (Release/retain?)
As a beginning iPhone programmer, what is the best practice for writing apps to be used either with iOS 5 or older versions? Specifically, should I continue using the release/retain of data, or should ...
108
votes
3
answers
15k
views
Override setter with arc
@interface Article : NSObject
@property (nonatomic, strong) NSString *imageURLString;
@end
@implementation Class
@synthesize imageURLString = _imageURLString;
- (void)setImageURLString:(NSString ...
97
votes
2
answers
27k
views
Does ARC support dispatch queues?
I'm reading apple's documentation about "Memory Management for Dispatch Queues":
Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and ...
97
votes
1
answer
46k
views
NSString to CFStringRef and CFStringRef to NSString in ARC?
I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC?
Same for going the opposite direction, CFStringRef to NSString in ARC?
What is the correct way to do this ...
96
votes
5
answers
92k
views
Weak and strong property setter attributes in Objective-C
What is the difference between weak and strong property setter attributes in Objective-C?
@property(retain, [weak/strong]) __attribute__((NSObject)) CFDictionaryRef myDictionary;
What is the impact ...
87
votes
3
answers
20k
views
KVO and ARC how to removeObserver
How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing?
For example,...
86
votes
7
answers
175k
views
Sending an HTTP POST request on iOS
I'm trying to send an HTTP Post with the iOS application that I'm developing but the push never reaches the server although I do get a code 200 as response (from the urlconnection). I never get a ...
86
votes
3
answers
50k
views
How do I replace weak references when using ARC and targeting iOS 4.0?
I've begun developing my first iOS app with Xcode 4.2, and was targeting iOS 5.0 with a "utility application" template (the one that comes with a FlipsideViewController).
I read that since ARC is a ...
85
votes
4
answers
53k
views
ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc
ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc?
Why is this so?
I had the assumption that if you mark it -fno-objc-arc you don't have this restriction.
84
votes
4
answers
44k
views
ARC - The meaning of __unsafe_unretained?
Just want to make sure that I got it right:
Do I need to __unsafe_unretain objects that I don't own?
If an object is __unsafe_unretained Do I need to use assign in the @property? Does that mean that ...
83
votes
2
answers
15k
views
Cannot use respondsToSelector using ARC on Mac
When I call respondsToSelector in an ARC environment, I get the following error message Automatic Reference Counting Issue No known instance method for selector respondsToSelector:
This is the header
...
83
votes
13
answers
100k
views
Is it possible to debug "Terminated due to memory error"?
In a certain (consistent) point when my app is running, I consistently get the xcode error message
Terminated due to memory error.
I cannot find the code causing the error, but I can tell what ...
80
votes
4
answers
33k
views
@property definitions with ARC: strong or retain?
Using Xcode 4.2 and ARC, I notice that the auto-generated code for an NSManagedObject still reads like this for properties:
@property (nonatomic, retain) NSString * someString;
1) Shouldn't retain ...
80
votes
2
answers
20k
views
How to enable ARC for a single file
I want to bring a single Objective-C class written using ARC into an old project. The internet provides many references for how to enable ARC for your project and then disable it for single files but ...
76
votes
4
answers
18k
views
What is the equivalent of @autoreleasepool in Swift?
In Swift, I notice there is no @autoreleasepool{} construct, although Swift does use ARC. What is the proper way to manage an autoreleasepool in Swift, or has it been removed for some reason?
72
votes
2
answers
27k
views
How to debug memory leaks when Leaks instrument does not show them?
I have an iOS app written in Swift that is leaking memory - in certain situation some objects should be released but they are not. I have learnt about the issue by simply adding deinit debug messages ...
71
votes
3
answers
28k
views
Generate a UUID string with ARC enabled
I need to generate a UUID string in some code with ARC enabled.
After doing some research, this is what I came up with:
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *uuidStr = (__bridge_transfer ...
70
votes
12
answers
47k
views
NSArray of weak references (__unsafe_unretained) to objects under ARC
I need to store weak references to objects in an NSArray, in order to prevent retain cycles. I'm not sure of the proper syntax to use. Is this the correct way?
Foo* foo1 = [[Foo alloc] init];
Foo* ...
64
votes
10
answers
35k
views
AVAudioPlayer not playing any sound
I'm working on an iOS application that needs to play some sounds using the AVFoundation framework. The workspace structure in Xcode 4 contains two projects:
Workspace
The application itself (main ...
62
votes
4
answers
24k
views
Should an NSString property under ARC be strong or copy?
When not compiling with ARC, it is recommended to use copy properties for data types such as NSString. I could not find proper documentation on the use of copy in ARC mode. Can someone tell me what's ...