私は何が起こっているのかを知るために数日を費やしました. 私は大量のメモリ管理ドキュメントを読みましたが、「リリースが必要なすべての割り当てについて」と聞いてうんざりしています-それは知っていますが、コードがメモリリークを生成している理由をまだ理解できません。
プロパティの 1 つとして NSMutableDictionary を持つ単純なカスタム クラスを作成しています。基本的に、XMLELement を模倣します。私は一生、辞書の割り当てがメモリリークを引き起こしている理由を理解できません。リークはデバイスとシミュレータで発生します - デバイスで 5 リーク、シミュレータで 20 リーク。
変数 *tmp を宣言して割り当てると、リークが発生します。
属性の詳細 (名前と値) を設定すると、リークもあります。
これは私を夢中にさせています。助けてください!
コードの一部:
@interface IMXMLElement : NSObject {
NSString *strElementName;
NSString *strElementValue;
NSMutableDictionary *dictAttributes;
}
@property (nonatomic, retain) NSString *strElementName;
@property (nonatomic, retain) NSString *strElementValue;
@property (nonatomic, retain) NSMutableDictionary *dictAttributes;
@end
@implementation IMXMLElement
@synthesize strElementName;
@synthesize strElementValue;
@synthesize dictAttributes;
-(id)initWithName:(NSString *)pstrName
{
self = [super init];
if (self != nil)
{
self.strElementName = pstrName;
**LEAK NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init];
self.dictAttributes = tmp;
[tmp release];
}
return self;
}
-(void)setAttributeWithName:(NSString *)pstrAttributeName
andValue:(NSString *)pstrAttributeValue
{
**LEAK [self.dictAttributes setObject:pstrAttributeValue forKey:pstrAttributeName];
}
-(void)dealloc
{
[strElementName release];
[strElementValue release];
[dictAttributes release];
[super dealloc];
}
次のコードを使用してこのクラスにアクセスします。
NSString *strValue = [[NSString alloc] initWithFormat:@"Test Value"];
IMXMLElement *xmlElement = [[IMXMLElement alloc] initWithName:@"Test_Element"];
[xmlElement setAttributeWithName:@"id" andValue:strValue];