Objective C Object Initialization

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

  1. Soulstorm
    Expand Collapse
    macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    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
    Expand Collapse
    macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    [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
    Expand Collapse
    macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    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
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000

Share This Page