objective-c - C代码中的内存泄漏

标签 objective-c c memory-leaks

我使用一些非 Objective-C 代码编写了此方法,仪器告诉我存在泄漏。不仅如此,free(char*) 也会崩溃......

如有任何帮助,我们将不胜感激。

谢谢

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 =(const char*)malloc(sizeof(uint32_t));
            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 = (const char*)malloc(sizeof(uint32_t));
                const char *type = (const char*)malloc(sizeof(uint32_t));

                name = property_getName(property);
                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];
                }
                free((char*)name);
                free((char*)type);
                free(property);
            }else{
                //NSLog(@"%@ did not respond to : %@", result, key);
            }

            free((void*)c);

        }

        //Return result
        return result;

最佳答案

崩溃是因为您分配了 c,然后用 cStringUsingEncoding: 返回的内容覆盖指针,然后尝试释放 cStringusingEncoding 返回的内容,您的代码不拥有。另外,原始指针也被泄露了。

来自 docs对于 cStringUsingEncoding :

The returned C string is guaranteed to be valid only until either the receiver is freed, or until the current memory is emptied, whichever occurs first. You should copy the C string or use getCString:maxLength:encoding: if it needs to store the C string beyond this time.

关于objective-c - C代码中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20257987/

相关文章:

ios - 获取容器 VC 而不是 View

iOS navigationbar setBackBarButtonItem 为自定义按钮

objective-c - 带有完成 block 的自定义 xml 解析器类

c - 在函数中分配多维数组

javascript - 一旦组件被销毁,在 initComponent 函数中创建的存储是否会发生内存泄漏,或者这些存储是否会被垃圾收集?

ios - 在 Storyboard中设置约束时,Xcode 不断崩溃

c - a+i 和 a[i] 有什么区别

c - 伪终端(pty)报告资源暂时不可用

c++ - C++/Qt - 内存分配如何工作?

c++ - 带有 std::shared_ptr 的视觉泄漏检测器