2

I am trying to convert an old code to an ARC code. However, i am getting a build error at the following line

[[token retain] autorelease];

I get 3 errors in the following order:

-'autorelease' is unavailable: not available in automatic reference counting mode

-[rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately

-ARC forbids explicit message send of 'autorelease'

If i remove this particular line, then the code compiles correctly

jerry
  • 274
  • 2
  • 19
  • http://stackoverflow.com/questions/9865847/is-it-possible-to-combine-non-arc-and-arc-project-code This ought to help. – ratbum Sep 07 '12 at 23:21
  • @ratbum that's correct. I have already done these steps, but when i go to "Edit" -> "Refactor" -> "Objective-c ARC" and then select the relevant file to convert and click "Check", then i get an error at this particular line – jerry Sep 07 '12 at 23:24

3 Answers3

6

If that is all that is on the line, you can probably just remove it. That code claims a reference to the object but also says you don't want to worry about releasing later, but with ARC you don't have to worry about it.

I am guessing that Xcode did not get rid of it automatically because of lack of context. If it had been in a more common location, such as in a return statement, it would be obvious what the intent was. When it is all by itself, it could be, but probably is not, something more complicated.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
4

ARC manages your memory for you, so you don't need to call "autorelease", "release", "retain" etc. With ARC the compiler retain and release objects as necessary for you so you don't have to explicitly make these calls. If you want to manually manage your memory disable ARC, or you can use the compiler flag "-fno-objc-arc" to disable ARC on a specific class.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Yeah, but if i am supposed to ask Xcode to convert my file to ARC, is it not it's responsibility to convert any references to "autorelease" or "release" or "retain" ... retain and release calls are already present in other parts of the file. But it is only this line that it shows an error – jerry Sep 07 '12 at 23:27
  • 1
    @jerry Just remove the extraneous "[" at the beginning of the line and " autorelease];" – Mick MacCallum Sep 07 '12 at 23:28
  • thanks a lot ... it's compiling now. Could you explain to me why it was giving an error ?? – jerry Sep 07 '12 at 23:31
  • @jerry I explained that in my post, the compiler was taught to show errors anywhere that you try to manually manager your memory when ARC is enabled to show you that you shouldn't be trying to do this. With ARC any retain/release calls are handled automatically. And if my post has helped you then please mark my post as correct by clicking the "check mark" next to my post. – Mick MacCallum Sep 07 '12 at 23:33
0

ARC means you don't have to use release, retain or those other pesky memory management things. ARC add them all for you automatically when you compile the app. Read more here

If you have old code where it would not be the best solution to remove all the memory management calls, then check this answer to disable ARC for particular files

Community
  • 1
  • 1
Wasim
  • 4,953
  • 10
  • 52
  • 87