[HELP] release issue !

Discussion in 'Mac Programming' started by Oneill, Nov 10, 2012.

  1. macrumors newbie

    Nov 10, 2012
    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 !
  2. macrumors 604

    Aug 9, 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).
  3. thread starter macrumors newbie

    Nov 10, 2012
    hey chown33 thanks for replying,
    Answering your two questions:
    1)Here is the 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 !
  4. macrumors regular

    Nov 9, 2011
    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:

    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.
  5. thread starter macrumors newbie

    Nov 10, 2012
    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?
    NSDate * date2 = [[NSDate alloc] myDate copy];
    <-- is this line written well?
  6. macrumors regular

    Nov 9, 2011
    It looks OK. Will it compile?
  7. macrumors 68040


    Jan 10, 2005
    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.



    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.
  8. thread starter macrumors newbie

    Nov 10, 2012
    thanks Lee for the obvious explanation that means a lot !

Share This Page