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 11

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


Admin

brandontreb

posts 88

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…

If debugging is the process of removing bugs, then programming must be the process of putting them in.
-Edsger Dijkstra

11:36 pm
September 22, 2008


corey

iCoder

posts 11

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


Admin

brandontreb

posts 88

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?

If debugging is the process of removing bugs, then programming must be the process of putting them in.
-Edsger Dijkstra

2:02 pm
October 7, 2008


VertigoSol

Moderator

posts 26

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

e^i -1 =0

Search 

About the iCodeBlog forum

Most Users Ever Online:

44


Currently Online:

4 Guests

Forum Stats:

Groups: 2

Forums: 6

Topics: 419

Posts: 893

Membership:

There are 934 Members

There has been 1 Guest

There are 2 Admins

There is 1 Moderator

Top Posters:

bobcubsfan – 54

crazyiez – 30

Uhu – 17

AdeC – 17

Nick – 15

jitesh61 – 12

Administrators: Brandon (88 Posts), Collin (0 Posts)

Moderators: VertigoSol (26 Posts)



©   

  • Posted by on 6 Aug 2008 in Uncategorized
  •   |  
  •   |  
  •   |  
  • Comments Off

Comments are closed.