Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sean Kim

macrumors newbie
Original poster
Jun 9, 2011
5
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
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?

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.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.