obj-c / c ++代码中的内存泄漏

时间:2013-11-28 05:44:36

标签: c++ ios objective-c

我正在使用乐器,这种方法会泄漏大量内存。它在我的应用程序中随处可用,当我构建它时,我使用了一些c ++(我认为)。

如果可能的话,请告诉我泄漏发生在哪里?

乐器只是说Malloc 16字节,并且有大约60 MB这些家伙......

/*
 Parses a dicionary into an object
 */
+(id)makeObject:(id)object fromDictionary:(NSDictionary *)dict{

    @autoreleasepool {
        NSArray *keys = @[@"@\"NSMutableArray\""];

        //Init result
        id result = object;

        //Iterate every key
        for (id key in [dict allKeys]) {

            //Convert key to const char
            const char * c = [key cStringUsingEncoding:NSASCIIStringEncoding];

            //Use c to see if the class has this property
            if (class_getProperty([object class], c)) {

                //get the property
                objc_property_t property = class_getProperty([result class], c);

                //Get the property name and type
                const char *name = property_getName(property);
                const char *type = property_copyAttributeValue(property, "T");

                //Cast const char to string
                NSString *pNameString = [NSString stringWithFormat:@"%s",name];
                NSString *typeString = [NSString stringWithFormat:@"%s",type];

                //Add relationships
                if ([keys containsObject:typeString]) {

                    //Get array of objects
                    NSArray *relationship = [dict objectForKey:pNameString];

                    NSMutableArray *allSubObjects = [[NSMutableArray alloc]init];
                    //Parse each individual object
                    for (NSDictionary *relationshipObj in relationship) {

                        //Create class from relationship
                        Class class = NSClassFromString(pNameString);

                        //Create object
                        id sub = [self makeObject:[[class alloc]init] fromDictionary:relationshipObj];

                        [allSubObjects addObject:sub];
                    }

                    [result setValue:allSubObjects forKey:pNameString];
                }else{
                    //If so set the property for the key
                    [result setValue:[dict objectForKey:key] forKey:key];
                }

            }else{
                //NSLog(@"%@ did not respond to : %@", result, key);
            }


        }

        //Return result
        return result;
    }

}

编辑:

仪器显示这两个项目很可能是罪魁祸首。解决这个问题的最佳方法是什么?

//Cast const char to string
                    NSString *pNameString = [NSString stringWithFormat:@"%s",name];
                    NSString *typeString = [NSString stringWithFormat:@"%s",type];

1 个答案:

答案 0 :(得分:1)

1)[NSString cStringUsingEncoding]

这会在内部分配一些内存,直到NSString对象被释放后才会释放。我不确切知道但是可能重复发送cStringUsingEncoding会导致未使用的内存增加吗?

2)property_copyAttributeValue

文档说,

  

返回值描述objc_property_t类型的C指针数组   proto声明的属性。其他人声明的任何属性   该协议采用的协议不包括在内。阵列   包含* outCount指针后跟一个NULL终止符。你必须   使用free()释放数组。

您需要免费const char *type

3)NSMutableArray *allSubObjects = [[NSMutableArray alloc]init]

应为NSMutableArray *allSubObjects = [NSMutableArray array]

4)对象创建

Class class = NSClassFromString(pNameString);    
//Create object
id sub = [self makeObject:[[class alloc]init] fromDictionary:relationshipObj];

我将此代码块放在自己的自动释放池中 [class alloc]init]也很可疑。