Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

Memory Management

User Post

9:00 am
September 22, 2008


corey

iCoder

posts 10

1

As in when to allocate and initialize or when to release.

I've read Apple's documentation, but it is still confusion to me when I need to allocate memory and when I don't.  Also it is unclear who should release it and when it is ok to do so.

Sometime variables in the documentation seem like they are used without any allocation.  If that is true, I'd be nice to know why that can happen and how to use that to my advantage.

12:33 pm
September 22, 2008


Brandon

Admin

brandontreb

posts 73

2

corey,


give me a specific example where you are having trouble understanding and maybe I can provide you some insight.  A lot of stuff is “situation specific” as when to release, instansiate, etc…

11:36 pm
September 22, 2008


corey

iCoder

posts 10

3

Good point, it seems very situation specific…..

These are quetions I am definitely having while going through the tutorials that you have posted.  Maybe the easy thing to do is go back through your tutorials…

For instance in:

http://icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/

you define an updateText action in the ViewBasedViewController.m.

You allocate memory to the “text” string only to pass the value to another object, “lblHello.text”.  (I believe you did this because it is convention)

If you didn't allocate, could you still use the text string, or would you not be able to store information in it?

I guess it boils back down to:

1. Do you always allocate memory to an object if your unsure?

2. Is it actually possible to use an object without allocating memory to it?


I guess this has ended up more of an Obj-C discussion than a tutorial request.  Sorry its in the wrong place.


11:54 am
September 23, 2008


Brandon

Admin

brandontreb

posts 73

4

Allocation is only necessary when we are building a NEW instance of an object.  Primitive variables (ints, floats, strings, etc…) do not need allocation as they are not objects(this incldues NSString).  If you don't allocate an object variable and just declare it, you can use it as a pointer to another object.


Ex:

NSObject obj1 =  [[NSObject alloc] init];

NSObject obj2;

We can call functions on obj1 like [obj1 foo] but not on obj2 because it has not been instanciated.  In objective-c this is done by calling some form of init.  In most other languages, this is done with the “new” operator.  We can however do this:


obj2 = obj1


now we can say [obj2 foo].  This does not make it a new object, it now simply points to the obj1 object.


Does this all help?

2:02 pm
October 7, 2008


VertigoSol

Moderator

posts 8

5

Rule of thumb if you are the owner of a object ie you call [Object alloc] on it then you are responsible for releasing the memory of that object with either [Object release] orat creation time calling [[Object alloc] autorelease] which will release it in int NSAutoreleasePool when it falls out of scope


About the iCodeBlog forum

Currently Online:

2 Guests

Maximum Online: 14

Forums:

Groups: 2

Forums: 6

Topics: 118

Posts: 394

Members:

There are 225 members

There are 1 guests


Brandon has made 73 posts

Top Posters:

bobcubsfan - 33

crazyiez - 30

Uhu - 17

Nick - 15

corey - 10

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)  

Share and Enjoy: