Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Oneill

macrumors newbie
Original poster
Nov 10, 2012
13
0
Hello folks ,
Im new to the Macintosh programming world.
I recently started programming simple codes and once I used the saved word 'release' for memory management I got this error:
(Once I wanted to release an unwanted object)
ARC forbids explicit message send of 'release'
'relase' is unavailable: not available in automatic reference counting mode.

Hopefully you can help..
Oneill !
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
1. Post your code.

2. When using ARC, you don't have to release. Indeed, direct use of release is forbidden (which is what your error message is telling you). All releasing is automatic. That what the "A" in ARC stands for.

3. If you're learning from a book or tutorial, exactly which one? And exactly where are you in the book or tutorial (i.e. which chapter and page, or which step of a multi-step tutorial).
 

Oneill

macrumors newbie
Original poster
Nov 10, 2012
13
0
hey chown33 thanks for replying,
Answering your two questions:
1)Here is the code:
Code:
        NSString * message = @"Hello";
        NSString * message2= [NSString stringWithFormat:@"The word is:%@",message];
        NSString * anothermessage = [[NSString alloc] initWithFormat:@"The Word is %@",message];
        
        NSDate * myDate = [NSDate date];
        NSDate * anotherdate = [NSDate dateWithTimeIntervalSince1970:1234213];
        NSDate * date2 = [myDate copy];
        
        NSLog(@"The results are %@ , %@ , %@ , %@ , %@ , %@" , message, message2 , anothermessage , myDate , anotherdate , date2);
        
        
        [anothermessage release];
        [date2 release];

         
        
        }

Secondly im currently learning from lynda.com videos/courses and iPhone Programming book(Memory Management Chapter 4 page 47) by OReilly !
 

Senor Cuete

macrumors 6502
Nov 9, 2011
423
30
I think that there's another error as well.

NSDate * date2 = [myDate copy];
[date2 release];[/CODE]

It looks like you didn't allocate space for date2. Shouldn't you do something like this:

Code:
NSDate * date2 = [NSDate alloc];

before you copy the other NSDate to it? If space isn't allocated for it you will be copying data to a random location or a nil pointer. Either one will crash your app.
 

Oneill

macrumors newbie
Original poster
Nov 10, 2012
13
0
I think that there's another error as well.



It looks like you didn't allocate space for date2. Shouldn't you do something like this:

Code:
NSDate * date2 = [NSDate alloc];

before you copy the other NSDate to it? If space isn't allocated for it you will be copying data to a random location or a nil pointer. Either one will crash your app.

that makes sense. btw I wrote the code as it was given to me (through lynda video).
so you are saying if i want to copy I first need to allocate a space in the memory then copying it?
Code:
NSDate * date2 = [[NSDate alloc] myDate copy];
<-- is this line written well?
 

Senor Cuete

macrumors 6502
Nov 9, 2011
423
30
that makes sense. btw I wrote the code as it was given to me (through lynda video).
so you are saying if i want to copy I first need to allocate a space in the memory then copying it?
Code:
NSDate * date2 = [[NSDate alloc] myDate copy];
<-- is this line written well?

It looks OK. Will it compile?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Senor Cuete: This isn't how it works. The code prwsented for copy is fine. copy will create a new object, including allocating memory. The object returned will be owned by the caller, so ownership will need to be relinquished. The exercise is trying to show which methods (in this case alloc and copy) return objects you own vs the factory methods that return autoreleased objects. When you are done with objects you own you relinquish ownership with release. If you need to take ownership of an autoreleased object you do so using retain.

It looks like the example is meant to demonstrate "classic" reference counting memory management, but you set up your project with ARC, which handles acquisition and relinquishing of ownership for you. This means you no longer send release, retain or autorelease. It sounds like you need to turn ARC off on your project if you want to practice memory management.

-Lee

Edit:

Code:
NSDate * date2 = [[NSDate alloc] myDate copy];
This is nonsense, Senor Cuete led you astray (I'm sure with the best intentions), and this won't compile, though plenty of incorrect code will compile.
 

Oneill

macrumors newbie
Original poster
Nov 10, 2012
13
0
Senor Cuete: This isn't how it works. The code prwsented for copy is fine. copy will create a new object, including allocating memory. The object returned will be owned by the caller, so ownership will need to be relinquished. The exercise is trying to show which methods (in this case alloc and copy) return objects you own vs the factory methods that return autoreleased objects. When you are done with objects you own you relinquish ownership with release. If you need to take ownership of an autoreleased object you do so using retain.

It looks like the example is meant to demonstrate "classic" reference counting memory management, but you set up your project with ARC, which handles acquisition and relinquishing of ownership for you. This means you no longer send release, retain or autorelease. It sounds like you need to turn ARC off on your project if you want to practice memory management.

-Lee

Edit:

Code:
NSDate * date2 = [[NSDate alloc] myDate copy];
This is nonsense, Senor Cuete led you astray (I'm sure with the best intentions), and this won't compile, though plenty of incorrect code will compile.

thanks Lee for the obvious explanation that means a lot !
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.