Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 10, 2012, 03:14 PM   #1
Oneill
macrumors newbie
 
Join Date: Nov 2012
[HELP] release issue !

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 !
Oneill is offline   0 Reply With Quote
Old Nov 10, 2012, 03:44 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
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).
chown33 is offline   0 Reply With Quote
Old Nov 11, 2012, 06:46 AM   #3
Oneill
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
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 !
Oneill is offline   0 Reply With Quote
Old Nov 11, 2012, 08:29 AM   #4
Senor Cuete
macrumors regular
 
Join Date: Nov 2011
I think that there's another error as well.

Quote:
Originally Posted by Oneill View Post
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.
Senor Cuete is online now   0 Reply With Quote
Old Nov 11, 2012, 08:42 AM   #5
Oneill
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by Senor Cuete View Post
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?
Oneill is offline   0 Reply With Quote
Old Nov 11, 2012, 09:14 AM   #6
Senor Cuete
macrumors regular
 
Join Date: Nov 2011
Quote:
Originally Posted by Oneill View Post
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?
Senor Cuete is online now   0 Reply With Quote
Old Nov 11, 2012, 09:20 AM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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.
lee1210 is offline   0 Reply With Quote
Old Nov 11, 2012, 09:27 AM   #8
Oneill
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by lee1210 View Post
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 !
Oneill is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Will Apple ever release a fix for the scrolling issue? zeiter MacBook Pro 59 Feb 27, 2014 09:51 AM
iPhone 5S release is a prelude to XBOX ONE/PS4 RELEASE. FooArk Console Games 26 Sep 21, 2013 10:42 PM
A small thought on the 12th September release. (W/ iPhone release) nicklaz0r iMac 42 Sep 6, 2012 08:12 PM
Would it be ethical for Apple to release false iPhone info leading up to a release? thermal iPhone 27 Aug 11, 2012 09:13 AM
Was OSX Lion's battery issue fixed since release? kettlecorn Mac OS X 10.7 Lion 0 Jul 5, 2012 02:43 AM

Forum Jump

All times are GMT -5. The time now is 09:04 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC