PDA

View Full Version : Objective C Object Initialization




Soulstorm
Nov 6, 2007, 03:04 AM
First of all, let's clarify that I'm talking about ObjC prior to version 2.0. So, there is no garbage collection.

In a foundation tool (command line objc program), what is the difference between instantiating an object using alloc-init, and using a class method like "[NSSTring stringWithString]" in terms of memory management?

And the same question applies for Cocoa applications.

Is there any reference I can read up on those things?



Nutter
Nov 6, 2007, 04:09 AM
[NSString stringWithString:string] is equivalent to [[[NSString alloc] initWithString:string] autorelease].

I'd recommend reading the Memory Management Programming Guide for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/).

This advice applies to foundation tools as well. (The Foundation framework is part of Cocoa.)

kpua
Nov 6, 2007, 08:16 AM
Under GC, there no longer is a difference between the convenience allocators and the regular allocators—except that they are still convenient.

With GC, -release, -autorelease, and -retain are just empty methods that take no effect.

kainjow
Nov 6, 2007, 11:16 PM
I'd recommend reading the Memory Management Programming Guide for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/).

More specifically, Autorelease Pools in Non-AppKit Programs (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/AutoreleasePools.html#//apple_ref/doc/uid/20000047-DontLinkElementID_11).