MyTestClass.hという名前のクラスがあるとします。
クラス構造は次のようになります
@interface MyTestClass : NSObject {
NSString *testString;
}
@property (nonatomic, retain)NSString * testString;
@end
.m ファイル
@implementation MyTestClass
@synthesize testString;
-(id) init{
[self setTestString:@""];
return self;
}
-(void)dealloc{
[self.testString release];
testString = nil;
[super dealloc];
}
@end
今、私はMyTestClassのオブジェクトを作成し、testStringを2 回割り当てました
MyTestClass * myTestClass = [[MyTestClass alloc] init];
[myTestClass setTestString:@"Hi"];
[myTestClass setTestString:@"Hello"];
今、私のtestStringsメモリが2回リークされていると思います!! (1 つはinit()を介して、もう 1 つは最初のsetTestStringメソッドを介して)
私は正しいですか?または、@property (nonatomic, retain)
以前に割り当てられたメモリを処理/解放しますか?
または、このような場合、以下のコードのようにMyTestClass.mのsetTestString()をオーバーライドする必要がありますか?
-(void)setTestString:(NSString *)tempString{
[testString release];
testString = nil;
testString = [tempString retain];
}
この質問に関するヘルプをいただければ幸いです。
ありがとう。