我遇到了我创建的单例问题。它包含两个NSMutableDictionary,它们在应用程序中的三个视图(以及一些模式视图)中被读取和使用。设置单身字典导致EXC_BAD_ACCESS
我已经在地图上的字典里添加了一个MKMapView t图的一些场地。当我使用完全相同的方法/函数在其他视图中访问数据时,我收到与释放字典有关的EXC_BAD_ACCESS错误。这来自NSZombieEnabled:
CFDictionary retain: message sent to deallocated instance
在dsym'ed跟踪,它与另一种引起悲伤更换一部字典。我使用调用该函数的代码来自一个MKAnnotationView点击:
UIControl *tempButton = sender;
NSString *selectedEventsString = [self.eventsArray objectAtIndex:tempButton.tag];
NSLog(@"eventString: %@", selectedEventsString);
[[EventsManager eventsManager] changeSelectedEventsDictionaryTo:selectedEventsString];
[tempButton release];
[selectedEventsString release];
“selectedEventsString”中走出来完全对应的事件。
在事件管理的相应的代码:
-(void)changeSelectedEventsDictionaryTo:(NSString *)eventName {
NSLog(@"singleton: %@", eventName);
self.eventString = eventName;
self.selectedEventsDictionary = [self.eventsDictionary objectForKey:eventName];
}
两个selectedEventsDictionary和eventsDictionary被设置为@属性(非原子,保留)在.H文件,这是初始化函数:
+ (EventsManager*)eventsManager {
if (eventsManager == nil) {
eventsManager = [[super allocWithZone:NULL] init];
eventsManager.eventsDictionary = [[NSMutableDictionary alloc] init];
eventsManager.selectedEventsDictionary = [[NSMutableDictionary alloc] init];
eventsManager.eventString = [[NSString alloc] init];
eventsManager.mode = [[NSString alloc] init];
}
return eventsManager;
}
这是在其他视图中使用的代码示例,其工作正常:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *eventString = [self.eventsArray objectAtIndex:row];
[[EventsManager eventsManager] changeSelectedEventsDictionaryTo:eventString];
//Modal display code here
}
任何帮助将不胜感激!我想我已经提供了所有相关的代码,但是如果需要更多的代码,请告诉我。
干杯!
好吧,刚刚解决它。但它真的很奇怪。在某些时候,我在MKAnnotationView中存储了一个额外的NSString来显示自定义数据。我停止使用它,但该变量仍然在.H文件中声明。当我删除它时,一切都落空了。 奇怪的,奇怪的错误。 – gamblor87 2011-04-08 23:39:19