尝试创建和填充 NSMutableDictionary 时出现内存错误
我不是 Cocoa 开发人员,但我一直在尝试为 PhoneGap 构建一些插件。这个特定的插件方法要么是 1) 使应用程序崩溃而不说明原因,要么是 2) 抱怨我如何释放/不释放对象。我已经尝试了很多东西,包括使用枚举器而不是 for 循环。如果有人能指出我正确的方向,那就太棒了。我不介意跑腿:
- (void)getPreferences:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
NSUInteger argc = [arguments count];
NSString* jsCallback = nil;
if (argc > 0) {
jsCallback = [arguments objectAtIndex:0];
} else {
NSLog(@"Preferences.getPreferences: Missing 1st parameter.");
return;
}
NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSMutableArray *keys = (NSMutableArray *) [options objectForKey:@"keys"];
NSMutableDictionary *values = [[NSMutableDictionary alloc] init];
NSUInteger ky = [keys count];
for (int i = 0; i < ky; i ++) {
@try {
[values setObject:[defaults objectForKey:[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]];
}
@catch (NSException * err) {
NSLog(@"Error %@", err);
}
}
[keys release];
NSString* jsString = [[NSString alloc] initWithFormat:@"%@(%@);", jsCallback, [values JSONRepresentation]];
[defaults release];
[values release];
[webView stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}
人类版本:
options
包含一个带有单个“keys”键的字典,- 该键包含一个字符串数组(将用作查找的键)
- 我想要循环遍历该数组和
- 对于该键的
defaults
中存在的每个值,使用相同的键将其复制到values
- 最后,我想要将
values
作为 JSON 发送回来(当我刚刚传递整个defaults
对象时,这部分正在工作,所以我认为 JSON 方法正在工作)
I am not a Cocoa developer, but I have been dabbling in it to build some plugins for PhoneGap. This particular plugin method is either 1) crashing the app without saying why or 2) complaining about how I release/don't release an object. I have tried a ton of things on my end, including using an Enumerator instead of the for loop. If anyone can point me in the right direction, that would be awesome. I don't mind legwork:
- (void)getPreferences:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
NSUInteger argc = [arguments count];
NSString* jsCallback = nil;
if (argc > 0) {
jsCallback = [arguments objectAtIndex:0];
} else {
NSLog(@"Preferences.getPreferences: Missing 1st parameter.");
return;
}
NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSMutableArray *keys = (NSMutableArray *) [options objectForKey:@"keys"];
NSMutableDictionary *values = [[NSMutableDictionary alloc] init];
NSUInteger ky = [keys count];
for (int i = 0; i < ky; i ++) {
@try {
[values setObject:[defaults objectForKey:[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]];
}
@catch (NSException * err) {
NSLog(@"Error %@", err);
}
}
[keys release];
NSString* jsString = [[NSString alloc] initWithFormat:@"%@(%@);", jsCallback, [values JSONRepresentation]];
[defaults release];
[values release];
[webView stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}
Human version:
options
contains a dictionary with a single key of "keys"- that key contains an array of strings (that are going to be used as keys for lookup)
- I want to loop through that array and
- For every value that exists in
defaults
for that key, copy it tovalues
using the same key - Finally, I want to send that
values
back as JSON (This part was working when I just passed the entiredefaults
object in, so I think the JSON method is working)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的代码中可以看出,您“拥有”对象
values
和jsString
(您使用alloc
创建的对象),因此您应该释放它们而不是任何其他。您可以在此处阅读有关内存管理的更多信息。
这是整个代码吗?另外,你到底得到了什么错误?
From your code, it follows that you 'own' objects
values
andjsString
(the ones you created withalloc
), so you should release them and not any other.You can read more on memory management here.
Is this the whole code? Also, what exactly error do you get?
Nikita 是对的,看起来你过度释放了默认值,这会在自动释放池释放时导致崩溃。另外,如果我理解您想要正确执行的操作,您可以使用一行代码创建
values
字典:Nikita is right, it looks as though you're overreleasing
defaults
, which would cause a crash later when the autorelease pool gets released. Also, if I understand what you're trying to do correctly, you could create thevalues
dictionary with a single line of code: