NSObject + JSONSerializableSupport中的内存泄漏

时间:2012-08-03 13:24:57

标签: iphone ios xcode memory-management memory-leaks

在我的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];

请建议解决方案或告诉我哪里出错了。

0 个答案:

没有答案