Memory managment..important?

Discussion in 'iOS Programming' started by ryans79, May 18, 2009.

  1. ryans79 macrumors regular

    Joined:
    Apr 12, 2009
    #1
    Hello!

    I have started learning objective C in the hopes of going on to develop for th iphone, i was just watching the screencast for "memory management" and am totally confused by the whole theory and examples....

    my question is this, do i have to really really really understand memory management for simple apps and games once i come into iphone programming or can i skip this one?

    °°
    Ryan
     
  2. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    Really really simple apps probably not as a lot of the classes you will come into contact with will be autorelease anyway, but with games you need to be on the ball with memory management. Best bet is to just bite the bullet and persevere til you understand it.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    It's not important. It's absolutely critical. Get it wrong, more or less at all in any way, and your app crashes. It's probably the most important thing to understand.
     
  4. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #4
    F***!
    Ok, gotto start reading externally and then go back to the screencast i guess.

    Will be doing a google search, but if anyone wants to contribute useful links, would appreciate it if you do.

    °° Ryan
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  6. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #7
    Found the first one via google, will go through it... then all the other links,
    °° guys!
     
  7. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #8
    Hey again,

    Been reading the links you guys sent me, i think the Apple docs goes a bit too far into detail so confuses the end user (me), esp if the end user first wants to "get it" and later go into the details of it.

    Tell me if I got this right, when creating an object using alloc, retain or copy its like bonking a woman... then she has a kid, and now you are responsible for it (till its 18years old - then you release it - i make up these things so i dont forget - yep, im nuts :eek: ) .... other peoples kids (objects) = not your responsibility so dont kick them outa the house ;)

    As you guys are programming in the real world and i am just learning (on a more serious note) how do you write your release?
    eg:
    As soon as you write your alloc etc you write your release and then the "meat" of the program or do you follow any other "personal rules"?

    I think using mental notes or writing your whole program then using instruments to find and plug leaks is not such a good idea... but hey, i am learning something new so would love to hear from you guys who are actually doing this on a daily basis.

    °°
    Ryan
     
  8. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #9
    A good little style convention I follow is if I'm in a method and I make a pointer to an object, I always prefix the pointer with "new" if I have to release it:

    NSArray *newArray = [[NSArray alloc] initWith. . . ];

    CustomObject *newCustomObject = [[CustomObject alloc] initWith. . . ];

    NSString *string = [@"First" stringByAppendingString: @" Last"];

    It just helps a little if you are looking through a long method.
     
  9. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #10
    The basic rule is mentioned everywhere: the number of allocs/copys/retains in a method has to be the same as the number of releases/autoreleases.
    this is not 100% true, but like 98% or something.

    Since not long ago I learned all this myself I know it's hard. but there is a point when it just clicks and you got it. when you got it once you can't really make much mistakes, it seems to simple then.

    the basic concept is really easy: when you don't need autorelease, don't use autorelease (even if it seems tempting!). if you create an object without autorelease, then simply release it when you don't need it anymore.
    if you receive an object that you need for later, retain it. it's that simple :D

    PS: best explanation for memory managment ever ><

    oh btw: memory managment is like the most difficult but also most important thing to get. even if you have a small app where you won't crash the app because of memory leaks - there will be a moment when your app crashes because you accessed an object that was autoreleased or stuff like that. finding these bugs, especially if you don't know memory managment, is a nightmare.
     
  10. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #11
    Ah! Thank you, I think i shall start using your logic, and if that does not work out to remind my dumb ass will start using release_me_dumbass instead of "new" ;)

    I'm not really used to this, i did C ages back and was not very good with pointers back then, Java had auto memory collection then i went into PHP and again this never came up.
    From what i see, the Mac has auto memory collection... but the iphone does not, if on the next update the iphone gets auto memory collection i dont know if i will want to kiss Apple or kick their ass for waiting so long to release it... after i learnt all this stuff.

    Guess i have a ways to go before i reach that stage..

    Heh! I dont have kids but like/love/adore etc women... - Its a case of opposites attracting, i'm into computers and use logic...
    I also found the thought of telling someone elses kid he/she's gotto move out hilarious... so i guess i'm not gonna forget that explanation.

    Kinda got the feeling it would be, thats why i started this thread to see how important it is.
    Trying to find something when you dont have a clue where it is kind of conjures up an image of Stevie Wonder and Ray Charles playing tennis...
     

Share This Page