gpt4 book ai didi

objective-c - 处理 NSJSONSerialization 中的 Foundation 对象时发生内存泄漏

转载 作者:行者123 更新时间:2023-12-03 16:40:27 26 4
gpt4 key购买 nike

我正在努力修复我制作的辅助函数中的内存泄漏。辅助函数获取

的结果
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError * _Nullable *)error 

并将所有叶子元素转换为 NSString(如果它们是 NSNumber)。

方法如下:

-(NSArray *) stringisizeObjects:(NSArray *)inputArray{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *mutable = [[NSMutableArray alloc] initWithCapacity:[inputArray count]];

for (int i = 0; i < [inputArray count]; i++) {
NSArray *keys = [inputArray[i] allKeys];

NSMutableDictionary *addDictionary = [[NSMutableDictionary alloc] initWithCapacity:[keys count]];

for (int j = 0; j < [keys count]; j++) {

id theObject = [[inputArray[i] objectForKey:keys[j]]autorelease];

if ([theObject isKindOfClass:[NSNumber class]]) {

[addDictionary setObject:[theObject stringValue] forKey:keys[j]];
[theObject release];

}else if ([theObject isKindOfClass:[NSString class]]){
[addDictionary setObject:[inputArray[i] objectForKey:keys[j]] forKey:keys[j]];
}

}
[mutable addObject:addDictionary];
}
NSArray *returnArray = [mutable copy];

[mutable removeAllObjects];
[mutable release];
[pool drain];
return returnArray;
}

这是我获取输入数组的方法。

id parsedThingy = [NSJSONSerialization JSONObjectWithData:resultJSONData options:1 error:&jsonDecodeError];

在将结果传递给 stringisize 方法之前,我必须确保我有一个带有匹配键的 NSDictionaries 的 NSArray。

NSArray *resultArray = [self stringisizeObjects:parsedThingy];

X-Code 内存泄漏工具已指出此方法是导致问题的原因。

Instruments showing leaks

正如你所看到的,我尝试将东西包装在自动释放池中,自动释放和释放。我只是看不到任何前进的方向。

这是一个 24/7 运行的非 ARC 项目。

编辑:我听取了 Droppy 的建议并尝试使用 mutableCopy 重写该方法。泄漏仍然存在。此时,我唯一的解决方法可能是更改 JSON 的源以仅发送字符串。 :(

-(NSArray *) stringisizeObjects2:(NSArray *)inputArray{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *mutableArray = [inputArray mutableCopy];

for (int i = 0; i < [mutableArray count]; i++) {
NSMutableDictionary *mutableDict = [mutableArray[i] mutableCopy];
NSArray *keys = [mutableDict allKeys];

for (int j = 0; j < [keys count]; j++) {
if ([[mutableDict objectForKey:keys[j]] isKindOfClass:[NSNumber class]]) {
NSString *stringValue = [[mutableDict objectForKey:keys[j]] stringValue];

[mutableDict removeObjectForKey:keys[j]];
[mutableDict setObject:stringValue forKey:keys[j]];
}
}
mutableArray[i] = [mutableDict copy];
[mutableDict release];
}

NSArray *returnArray = [mutableArray copy];

[mutableArray release];
[pool drain];
return returnArray;
}

最佳答案

问题:

  1. addDictionary 调用了 alloc 但未调用 releaseautorelease
  2. returnArray = [可变副本];//确实增加了retainCount +1,这里需要autorelease
  3. id theObject = [inputArray[i] objectForKey:keys[j]];//不需要自动释放或释放不属于您的对象
  4. 将 NSAutoreleasePool 添加到此处的顶部和底部,什么也不做

解决方案:

-(NSArray *) stringisizeObjects:(NSArray *)inputArray{
NSMutableArray *mutable = [[NSMutableArray alloc] initWithCapacity:[inputArray count]];

for (int i = 0; i < [inputArray count]; i++) {
NSArray *keys = [inputArray[i] allKeys];

NSMutableDictionary *addDictionary = [[NSMutableDictionary alloc] initWithCapacity:[keys count]];

for (int j = 0; j < [keys count]; j++) {

id theObject = [inputArray[i] objectForKey:keys[j]]; // not need autorelease

if ([theObject isKindOfClass:[NSNumber class]]) {

[addDictionary setObject:[theObject stringValue] forKey:keys[j]];
//[theObject release]; // not need release value here

}else if ([theObject isKindOfClass:[NSString class]]){
[addDictionary setObject:[inputArray[i] objectForKey:keys[j]] forKey:keys[j]];
}

}
[mutable addObject:addDictionary];
[addDictionary release]; // release after not use
}
NSArray *returnArray = [[[NSArray alloc] initWithArray:mutable] autorelease]; // auto release for return value

[mutable removeAllObjects];
[mutable release];
return returnArray;
}

关于objective-c - 处理 NSJSONSerialization 中的 Foundation 对象时发生内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com