Stacks & Memory Management.
Stacks & Memory Management.
- Subject: Stacks & Memory Management.
- From: Bill Bumgarner <email@hidden>
- Date: Sun, 21 Dec 2008 09:15:18 -0800
On Dec 21, 2008, at 8:21 AM, Steve Wetzel wrote:
Thanks Mike, I thought about it a long time before posting, then
posted, then when going to bed realize the first part of what you
said. I don't need to create separate instances to push 100 objects
onto the stack.
Actually, what Mike said was that you very much *do* need to create
separate objects to push 100 objects onto the stack. That cannot be
avoided.
The separate instances will either be allocated prior to being pushed:
MyObject *myObject;
myObject = [MyObject objectWithID: @"Bob"];
[stack push: myObject];
myObject = [MyObject objectWithID: @"Fred];
Or they will be copied within your -push: implementation:
...
- (void) push: (MyObject *) anObject
{
MyObject *newObject = [anObject copy];
... stick newObject into your stack data structure. -copy
implies -retain...
}
NOW I am trying to get my head around memory management with my
little stack. If anyone has insights and wants to share them feel
free. Instead of posting a question I think I will load up my
little app, look at retain counts, and try to figure out how to
stop all my memory leaks.
Do not look at the retain counts. Down that path lies madness.
Frankly, -retainCount should be removed (or, better, modified to just
return rand()).
Start here to understand your first question:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/chapter_1_section_1.html
Then read this to understand memory management:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
If you are developing new code, I would encourage you to turn on
garbage collection.
b.bum
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden