2

iPad アプリケーションで実行時のメモリ リークを除去しているときに、次のメソッドの NSObject+JSONSerializableSupport クラスでこの奇妙なメモリ リークに遭遇しました。

+ (id) deserializeJSON:(id)jsonObject {
id result = nil;
if ([jsonObject isKindOfClass:[NSArray class]]) {
    //JSON array
    result = [NSMutableArray array];
    for (id childObject in jsonObject) {
        [result addObject:[self deserializeJSON:childObject]];
    }
}
else if ([jsonObject isKindOfClass:[NSDictionary class]]) {
    //JSON object
    //this assumes we are dealing with JSON in the form rails provides:
    // {className : { property1 : value, property2 : {class2Name : {property 3 : value }}}}
    NSString *objectName = [[(NSDictionary *)jsonObject allKeys] objectAtIndex:0];

    Class objectClass = NSClassFromString([objectName toClassName]);
    if (objectClass != nil) {
        //classname matches, instantiate a new instance of the class and set it as the current parent object
        result = [[[objectClass alloc] init] autorelease];
    }

    NSDictionary *properties = (NSDictionary *)[[(NSDictionary *)jsonObject allValues] objectAtIndex:0];

    NSDictionary *objectPropertyNames = [objectClass propertyNamesAndTypes];

    for (NSString *property in [properties allKeys]) {
        NSString *propertyCamalized = [[self convertProperty:property andClassName:objectName] camelize];
        if ([[objectPropertyNames allKeys]containsObject:propertyCamalized]) {
            Class propertyClass = [self propertyClass:[objectPropertyNames objectForKey:propertyCamalized]];
            [result setValue:[self deserializeJSON:[propertyClass deserialize:[properties objectForKey:property]]] forKey:propertyCamalized];
        }
    }
}
else {
    //JSON value
    result = jsonObject;
}
return result;

}

この行でメモリリークが発生しています

[result setValue:[self deserializeJSON:[propertyClass deserialize:[properties objectForKey:property]]] forKey:propertyCamalized];

解決策を提案するか、どこが間違っているか教えてください。

4

0 に答える 0