PDA

View Full Version : alloc - no init - release --> no error




Sean Kim
Jun 28, 2011, 12:35 PM
in this reference page..
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/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)


#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...

#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
Jun 28, 2011, 12:48 PM
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 (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html) 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
Jun 28, 2011, 04:21 PM
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.