When I run Xcode 4.5 , By default it is with ARC Off stage.But When we create Xcode project
Strong property comes instead of Retain.
1)If Strong and weak related to ARC , why is it coming in non-Arc project?
2)If Both strong and retain are same, If I change the keyword retain to strong in earlier
xcode versons for non-Arc projects,will it work correctly?
Yes, strong is just a synonym for retain when not using ARC - it's included in non-ARC projects by default to minimize the changes needed when converting between ARC and non-ARC, since retain doesn't work with ARC but strong works with both project types.
Also, yes, if you change it to retain, that won't break your code.
In addition to H2CO3 answer´s: xCode will not warn you about this situation. Indeed, if you are adding this class from ARC implemented project , remember to implement -(void)dealloc() method and release delegates in case of protocol implementation.
Related
I'm making my first app and it's really confusing.
I am using ARC, and probably ALL of my potential leaks say:
"Object leaked: object allocated and stored in 'point' is not referenced later in this execution path and has a retain count of +1"
They are on almost any object I create using [[alloc] init].
Any suggestions about how to handle those? The app works fine, though.
Thanks.
Are you sure your project is actually set to ARC? Those types of analyzer warnings sound like it isn't. Is this a new project where ARC was automatically turned on for you or did you do the conversion for it yourself?
There isn't a way to actually tell the analyzer that you are ARC or aren't. It will just pick that up from the settings automatically.
I have this retained property declared like this:
#property (nonatomic, retain) NSMutableDictionary *codes;
then I synthesize this:
#synthesize codes;
I use the property like this:
self.codes = [NSMutableDictionary dictionary];
Then, I forget to say [codes release]; in my dealloc.
When I run Analyzer in XCode 4.3.2, this is not shown as an issue. My base SDK is iOS 5.1 and my compiler is Apple LLVM compiler 3.1
Why doesn't analyzer pick this up?
I imagine it's because the analyzer can't reliably detect retain/release issues across method/library boundaries.
You could conceivably pass ownership of your codes array to some external method or library which will release it later on for you. This would be bad practice because the receiving method should just retain it if it needs it, but I've seen this kind of thing done by inexperienced developers.
So you might see this in your class somewhere:
[SomeAPI takeThisArrayAndReleaseItLater:codes];
The analyzer has no way to know that your class is no longer responsible for releasing the array. To give you a warning would be incorrect, despite the fact that you are not following good memory management practices.
The analyzer is very good at only warning on real issues. I don't think I've ever seen a false-positive outside of betas builds, which is a good thing.
If you havent change anything from the configuration, whenver you target ios5+ you will automatically be using ARC (Automatic Reference Counting) which doesnt require you to release or retain.
The most disruptive change in iOS 5 is the addition of Automatic
Reference Counting, or ARC for short. ARC is a feature of the new LLVM
3.0 compiler and it completely does away with the manual memory management that all iOS developers love to hate.
This is a post by iOS Tutorial Team member Matthijs Hollemans, an experienced iOS developer and designer.
Tried to convert an iOS app using AdWhirl to ARC (Automatic Reference Counting).
There are several autorelease calls within AdWhirlView.m that ARC forbids.
When I refactored all my paid apps (that didn't have adwhirl) to ARC, XCode just removed my retains, releases, and autoreleases for me, but something about the way the code in AdWhirlView.m is written has caused XCode to pause, noting "[rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately."
Is anybody able to rewrite AdWhirlView.m to work correctly with ARC?
Or will I be forced to avoid combining the two? =/
Thanks in advance!
Alright, I've found the way around this...
When you click "Convert to Objective-C ARC" it brings up a menu called "Select Targets to Convert"
All you have to do is expand the list of files by clicking on the drop-down arrow, then deselect all the AdWhirl files.
XCode will convert the rest of your project to ARC without hitting on an error when it gets to the AdWhirl files.
How does a constant declared as below in the implementation part of a class is released:
static NSString *myconst = #"some data...";
Thx for helping,
Stephane
No, you don't need to release strings created with #"". You only need to release objects created with alloc, retain, copy or new.
You don't have to release it. The string literals reside in the executable's data section, not in the dynamically allocated memory (AKA heap).
There's no harm in accidentally calling release though. I'm pretty sure the literals are wired to quietly ignore that call.
retain, release and autorelease messages to strings of the above kind are ignored.
Read Apple's memory management docs here
However one thing to note here is passing release crashes the app. Hence the usual idea being, if you haven't used alloc or retain on the string, don't attempt to release it.
Also read this useful link here which explains the same thing.
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 compile-time feature, it should be compatible with iOS 4 as well, so I attempted to target my app to 4.3, and try compiling it. When I do so, I get this error:
FlipsideViewController.m: error: Automatic Reference Counting Issue: The current deployment target does not support automated __weak references
It is referencing this line:
#synthesize delegate = _delegate;
That variable is declared as:
#property (weak, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;
I understand that "weak references" are not supported in iOS 4, but I don't really understand why I would want to use a weak reference to begin with, nor can I figure out how I would rewrite things to avoid using it, while still taking advantage of ARC (after all, it's supposed to work with iOS 4 AND 5 right?)
To target the older OS, you can use unsafe_unretained instead of weak in your property declaration, and it should mostly work the same way. weak references nil themselves when their target goes away, but unsafe_unretained leaves open the possibility that the object you're linking to could turn into a dangling pointer when it is deallocated. The latter is the same behavior as if you had used assign as a property declaration in manual memory management.
You do this to avoid retain cycles, which I mention in my answer here. You don't want to have a strong pointer to something that might have a strong pointer back to the original object. Then nothing would get released properly.
If only using weak references for additional safety, manually call the new runtime functions if they're available and fallback to simple assignment on __unsafe_unretained variables if not.
ZWRCompatibility.h will simplify this somewhat.
Thanks to Mike Ash's compatibility library PLWeakCompatibilty, you can now simply use __weak on iOS 4.x, as well.
It's incredibly easy to configure and requires no additional consideration or effort over 5.x.