Sorry if this is an duplicated entry, I am new to OC.
I have turned off "Object-C Automatic reference counting" in build settings. And I have two classes Guitar
and GuitarController
.
GuitarController.m
looks like this:
#import "GuitarController.h"
@implementation GuitarController
-(void) setGuitar:(Guitar*) newGuitar
{
guitar = newGuitar;
// Yes, i did not retain the guitar object.
// I did it on purpose to test whether something will go wrong
}
-(void) playGuitar
{
[guitar play];
}
@end
and Guitar.m
looks like this:
#import "Guitar.h"
@implementation Guitar
-(void) play
{
NSLog(@"play guitar!!!");
}
@end
finally, the main.m
code:
#import <Foundation/Foundation.h>
#import "GuitarController.h"
int main(int argc, const char * argv[])
{
Guitar* guitar = [[Guitar alloc] init];
GuitarController* guitarController = [[GuitarController alloc] init];
[guitarController setGuitar:guitar];
[guitar release];
[guitarController playGuitar]; // Expecting an error here
return 0;
}
The code above works just fine. But it is obviously wrong because I referenced an Object after its reference count became 0. Any clues? Thanks!