difference between two kinds of assignments?

Discussion in 'iOS Programming' started by sujithkrishnan, Aug 18, 2008.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    What is the difference between

    NSString *str = @"hello";

    NSString *str = [NSString stringWithFormat:mad:"hello"];

    NSString *str = [NSString alloc]initWithString:mad:"hello"];

    If the 2nd is a convenient constructor, why we want to use/bother about "alloc-init" combination?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The third will have a retain count of 1 so will stick around until released. The second will be autoreleased and will get deallocated from RAM at the next autorelease pool flush, normally at the end of this run loop.

    Really you should read and understand the memory management guide before writing any code.
     
  3. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #3
    I went through the docs...
    Still i am not getting the diff in memory level....
    I also noticed that for some types eventhough i gave

    newObject =[oldObject copy]

    both newObject and oldObject are sharing same pointer....

    I got a sugestion to use mutableCopy... (Why copy didint worked?)

    Thanks....
    ( I wish GC should be in iPhone also...)
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Then read it again. Understanding the difference between retained and autoreleased objects is critical.

    I imagine this is a performance optimisation in Apples code. There is no reason that copying an immutable string (for example) should return a new object: nothing can be done to alter the string so the copy pointing at the original object is fine as long as the original object gets it's retain count incremented. Unless you actually need a mutable copy don't ask for one: you will use more RAM and that is a precious resource on the iPhone.

    I don't find the lack of GC to be an issue at all: managing your own memory is very easy once you learn the basic rules.

    If the Apple document is too complicated (and it's really not), perhaps you should read this.
     
  5. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #5
    Okie....

    What care we want to take for objects which will get initialized with Unarchived files???

    say...

    myObj =[NSKeyedUnarchiver unarchiveObjectWithFile:myFilePath];

    the 'myObj' is going invalid at some part of code....

    Is this stmnt is enough or i want to explicitily alloc it before coming to above line??
     
  6. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #6
    Okie....

    What care we want to take for objects which will get initialized with Unarchived files???

    say...

    myObj =[NSKeyedUnarchiver unarchiveObjectWithFile:myFilePath];

    the 'myObj' is going invalid at some part of code....

    Its not invalid through out the application... But in applicationWillTerminate: i am checking object and found its invalid...

    At which point, the autorelease objects will get released??
    Is it before coming to applicationWillTerminate method???

    Is this stmnt is enough or i want to explicitily alloc it before coming to above line??
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Does this method use the word init or copy? No? Then the returned object is autoreleased. Once again read and understand the links above. They make this rule very clear.

    alloc will do nothing: all that does is allocate memory which you don't want to do. At best calling alloc will leak memory, at worst it will cause your application to crash.

    As with every other autoreleased object if you want it to stick around you need to retain it to take ownership of it.

    Seriously: if you can't understand this having read the memory management documents you should probably not be writing software.
     
  8. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #8
    Hi robbie,
    Thanking You for such a valuable ADVICE...
    Its a "GREAT INSPIRATION AND ADVICE :eek:" for those who are coming to this field...
    Please understand that no one is born with loaded-knowledge of Mem-mgmnt.

    Still i am thanking you for the support..
     

Share This Page