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

ryans79

macrumors regular
Original poster
Apr 12, 2009
226
0
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
 
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.
 
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.
 
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
 
Found the first one via google, will go through it... then all the other links,
°° guys!
 
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
 
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"?

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.
 
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

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.
 
I always prefix the pointer with "new" if I ...

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.

when you got it once you can't really make much mistakes, it seems to simple then.

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

PS: best explanation for memory managment ever ><
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.

finding these bugs, especially if you don't know memory managment, is a nightmare.

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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.