alloc - no init - release --> no error

Discussion in 'Mac Programming' started by Sean Kim, Jun 28, 2011.

  1. macrumors newbie

    Joined:
    Jun 9, 2011
    #1
    in this reference page..
    http://developer.apple.com/library/...ols/NSObject_Protocol/Reference/NSObject.html
    it says
    "You must complete the object initialization (using an init method) before invoking release."

    but I tried code below, I got no errors (with -Wall compile option)

    Code:
    #import <Foundation/NSObject.h>
    #import <stdio.h>
    
    @interface MyObject : NSObject {
    	int var;
    }
    -(void) set: (int) arg;
    -(int) get;
    @end
    
    @implementation MyObject 
    -(void) set: (int) arg {
    	var = arg;
    }
    -(int) get {
    	return var;
    }
    @end
    
    int main(int argc, const char *argv[]) {
    	id anObject = [MyObject alloc];
    	[anObject release];
    	return 0;
    }
    and I tried another...

    Code:
    #import <Foundation/NSObject.h>
    #import <stdio.h>
    
    @interface MyObject : NSObject {
    	int var;
    }
    -(void) set: (int) arg;
    -(int) get;
    @end
    
    @implementation MyObject 
    -(void) set: (int) arg {
    	var = arg;
    }
    -(int) get {
    	return var;
    }
    @end
    
    int main(int argc, const char *argv[]) {
    	id anObject = [MyObject alloc];
    	printf("addr: %p\n", anObject);
    	[anObject set: 5];
    	[anObject release];
    
    	anObject = [MyObject alloc];
    	printf("addr: %p\n", anObject);
    	printf("var: %d\n", [anObject get]);
    
    	return 0;
    }
    
    
    result:
    addr: 0x100108ec0
    addr: 0x100108ec0
    var: 0
    I thought "alloc" is just reserve memory and "init" will do something like wipe out that memory with zeros.
    But result above shows that alloc zero out the memory.
    What kind of work is done by NSObject's init method?

    Thanks in advance~


    system info:
    i686-apple-darwin10-gcc-4.2.1
    mac osx 10.6.7
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    See The Objective-C Programming Language guide. In particular, look at the "Allocating and Initializing Objects" section:
    The alloc and allocWithZone: methods initialize a newly allocated object’s isa instance variable so that it points to the object’s class (the class object). All other instance variables are set to 0. Usually, an object needs to be more specifically initialized before it can be safely used.

    This initialization is the responsibility of class-specific instance methods that, by convention, begin with the abbreviation “init”. If the method takes no parameters, the method name is just those four letters, init. If it takes parameters, labels for the parameters follow the “init” prefix. For example, an NSView object can be initialized with an initWithFrame: method.
     
  3. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    In practice, you can occasionally "get away with" stuff that isn't supposed to work, due to details of the implementation. In this case, -init on NSObject doesn't actually do anything at the moment. It's easy to imagine a situation in which that could change, though, and any code relying on that fact would then break.

    TLDR: -[NSObject init] does nothing, but you can't rely on that.
     

Share This Page