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