Objective C Object Initialization

Discussion in 'Mac Programming' started by Soulstorm, Nov 6, 2007.

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    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?
  2. Nutter macrumors 6502

    Mar 31, 2005
    London, England
    [NSString stringWithString:string] is equivalent to [[[NSString alloc] initWithString:string] autorelease].

    I'd recommend reading the Memory Management Programming Guide for Cocoa.

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

    Jul 25, 2006
    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.
  4. kainjow Moderator emeritus


    Jun 15, 2000

Share This Page