2

I read on the web that when I create an object with alloc and init I have to release it (even a NSString), so:

Why if I create a NSString this way:

NSString *prova = [[NSString alloc] init];
[prova release];

I get these errors:

'release' is unavailable: not available in automatic reference counting mode

and

ARC forbids explicit message send of 'release'

on the [prova release] message? I get no error when I try do this:

NSString *prova = [[NSString alloc] init];
NSLog(@"Contenuto 0 di prova: %@", prova);
prova = @"prima prova stringa";
NSLog(@"Contenuto 1 di prova: %@", prova);
prova = @"ma cosè questo fantomatico errore";
NSLog(@"Contenuto 2 di prova: %@", prova);
elitalon
  • 9,191
  • 10
  • 50
  • 86
iLeW
  • 59
  • 10
  • 2
    Already answered in: http://stackoverflow.com/questions/6692022/why-cant-i-release-an-object-anymore – Omtara Nov 17 '11 at 10:01
  • Refer this link http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work – iamsult Nov 17 '11 at 10:03
  • http://stackoverflow.com/questions/6368600/some-questions-about-automatic-reference-counting-in-ios5-sdk/6368692#6368692 – iamsult Nov 17 '11 at 10:07

4 Answers4

5

That's a best practice previous to iOS 5, or in iOS 5 if ARC mode is disabled. Now iOS 5 uses the new Apple's LLVM compiler, which introduces this ARC feature.

So if ARC is enabled (and it is by default), you do not need to use, in general, the release method anymore. You can find more details in documentation.

If you still want to develop the old way, you can add the flag -fno-objc-arc in "Build phases" section of a Xcode project

elitalon
  • 9,191
  • 10
  • 50
  • 86
  • Thank you too. Is for this reason that there is not anymore the problem of the memory leak? For instance, here i can now change the string without problem. However, i think NSString was an immutable object, why i can change the string once i decided it through the assignment? – iLeW Nov 17 '11 at 16:01
  • @iLew You can find some answers to that in this question: http://stackoverflow.com/q/3428228/592454 – elitalon Nov 17 '11 at 16:25
4

You are using Apple's new ARC (automatic reference counting). ARC is a new compiler function which adds retain, release and autorelease on compile time automatically.

Have a look at the iOS 5 Release Nodes for more information about ARC: http://developer.apple.com/technologies/ios5/

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

miho
  • 11,765
  • 7
  • 42
  • 85
  • Thank you so much! I have only read the Objective C guide (and i'm reading the cocoa guide) and it seem that this is not written in this documents. How about the little code i've added in my question about memory leak. I think i couldn't do that. Thank you. – iLeW Nov 17 '11 at 10:13
  • If you want to follow your Cocoa Guide you could disable the ARC in the Build Settings, but if you plan a new project yourself I would recommend to use this feature. – miho Nov 17 '11 at 15:57
0

in ios 5 no need to release objects it automatically release your object. or another way disable "Objective-C Automatic Reference Counting" ARC from your xcode

Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33
0

ARC is the new feature in IOS5 which means Automatic reference counting... You dont need to look after release and all as you using arc in your app .. You can de-select when you start a new project in the checkbox.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115