
Untitled
By: a guest on
Aug 11th, 2012 | syntax:
None | size: 1.12 KB | hits: 5 | expires: Never
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface Hoge : NSObject
{
id object;
}
- (id)init;
- (void)replaceSelf;
@property (readonly) id object;
@end
@implementation Hoge
@synthesize object;
- (id)init
{
self = [super init];
if (self)
{
object = [[NSObject alloc] init];
}
return self;
}
- (void)replaceSelf
{
self = [self init]; // init以外のインスタンスメソッドでselfを上書きする
}
- (void)dealloc
{
[object release];
[super dealloc];
}
@end
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Hoge *hoge = [[Hoge alloc] init];
NSLog(@"%@ -- %@", hoge.description, hoge.object);
[hoge replaceSelf];
NSLog(@"%@ -- %@", hoge.description, hoge.object);
[hoge release];
[pool release];
getchar();
return 0;
}
// 実行すると↓みたいな感じになる.
// hoge.objectがメモリリークしそうな気がするけど…
// 2012-03-19 21:18:17.558 hoge[18860:707] <Hoge: 0x101314520> -- <NSObject: 0x101314590>
// 2012-03-19 21:18:17.563 hoge[18860:707] <Hoge: 0x101314520> -- <NSObject: 0x7ff20a500170>