NSMutableDictionary 导致 EXC_BAD_ACCESS
我正在尝试修复当我访问 tableView:cellForRowAtIndexPath:indexPath 中的 NSMutableDictionary 时引发的 EXC_BAD_ACCESS 错误。现在,当我使用 loadHistoryFromDBExtended 方法填充ridesDict时,它会起作用:
self.ridesDict = [self loadHistoryFromDBExtended];
NSLog(@"rides dict %@", self.ridesDict);
但是,我不想为正在加载的每个单元格调用[self loadHistoryFromDBExtended],因为字典不会改变,所以我尝试移动:
self.ridesDict = [self loadHistoryFromDBExtended];
到viewDidLoad,现在我 时,出现 EXC_BAD_ACCESS 错误
NSLog(@"rides dict %@", self.ridesDict);
当我使用: in tableView:cellForRowAtIndexPath:indexPath 。从我读到的内容来看,我似乎遇到了一些内存保留/释放问题,但我似乎无法弄清楚。在调用 loadHistoryFromDBExtended 方法后,我在 viewDidLoad 中尝试了 [self.ridesDict keep] 但这没有帮助。我对此很陌生,所以我很感激任何关于去哪里的指示。
编辑:这是 loadHistoryFromDBExtended 方法:
-(NSMutableDictionary *)loadHistoryFromDBExtended
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
FMDatabase* db = [FMDatabase databaseWithPath:[self getDBPath]];
if (![db open])
{
NSLog(@"Could not open db.");
[pool release];
}
//get users
FMResultSet *rs = [db executeQuery:@"SELECT * FROM R order by date desc"]; //query provides result set
//create result array
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
while ([rs next])
{
NSMutableArray *usersDictArray = [NSMutableArray array];
//look up names for id's
[usersDictArray addObject:[rs stringForColumn:@"rNames"]];
[usersDictArray addObject:[rs stringForColumn:@"dName"]];
[usersDictArray addObject:[rs stringForColumn:@"date"]];
[myDictionary setObject:usersDictArray forKey:[rs stringForColumn:@"rID"]];
[usersDictArray release];
}
//return usersArray;
return myDictionary;
[myDictionary release];
[pool drain];
}
I'm trying to fix an EXC_BAD_ACCESS error that is being thrown when i access a NSMutableDictionary in tableView:cellForRowAtIndexPath:indexPath. Right now it works when I fill ridesDict with a method loadHistoryFromDBExtended like so:
self.ridesDict = [self loadHistoryFromDBExtended];
NSLog(@"rides dict %@", self.ridesDict);
However, I don't want to call [self loadHistoryFromDBExtended] for every cell being loaded since the dictionary won't change so I tried moving:
self.ridesDict = [self loadHistoryFromDBExtended];
to viewDidLoad and now I am getting the EXC_BAD_ACCESS error when i use:
NSLog(@"rides dict %@", self.ridesDict);
in tableView:cellForRowAtIndexPath:indexPath. From what i've read it seems that i've got some memory retain/release issues but i can't seem to figure it out. I've tried [self.ridesDict retain] in viewDidLoad after the loadHistoryFromDBExtended method was called but that didn't help. I'm very new to this so I'd appreciate any pointers as to where to go.
Edit: Here is the loadHistoryFromDBExtended method:
-(NSMutableDictionary *)loadHistoryFromDBExtended
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
FMDatabase* db = [FMDatabase databaseWithPath:[self getDBPath]];
if (![db open])
{
NSLog(@"Could not open db.");
[pool release];
}
//get users
FMResultSet *rs = [db executeQuery:@"SELECT * FROM R order by date desc"]; //query provides result set
//create result array
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
while ([rs next])
{
NSMutableArray *usersDictArray = [NSMutableArray array];
//look up names for id's
[usersDictArray addObject:[rs stringForColumn:@"rNames"]];
[usersDictArray addObject:[rs stringForColumn:@"dName"]];
[usersDictArray addObject:[rs stringForColumn:@"date"]];
[myDictionary setObject:usersDictArray forKey:[rs stringForColumn:@"rID"]];
[usersDictArray release];
}
//return usersArray;
return myDictionary;
[myDictionary release];
[pool drain];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我写这篇博客是为了帮助理解和调试 EXC_BAD_ACCESS
http://loufranco.com/blog/files /Understanding-EXC_BAD_ACCESS.html
按照容易程度的顺序
运行构建并分析 - 您是否获得了干净的构建?看看它说的是什么,但您现在可以忽略泄漏问题 - 查找向已发布对象发送消息的问题
使用 NSZombiesEnabled 运行 - 这使得对象永远不会释放,然后如果将消息发送到对象则抱怨keepCount 为 0。
启用 Guard Malloc,然后使用特殊的 GDB 命令来检查堆的完整性。问题是你需要在崩溃之前逐步执行此操作以找到真正的问题。 但它可能会在更接近您的问题的其他地方崩溃
I wrote this blog to help understand and debug EXC_BAD_ACCESS
http://loufranco.com/blog/files/Understanding-EXC_BAD_ACCESS.html
In order of easiness
Run a Build and Analyze -- do you get a clean build? Look at what it's saying, but you can ignore leak problems for now -- look for problems of sending messages to released objects
Run with NSZombiesEnabled -- this makes objects never deallocate, and then complain if a message is sent to an object with retainCount of 0.
Enable Guard Malloc and then use the special GDB commands to inspect the integrity of the heap. The problem is that you need to step through and do this before you crash to find the real problem. It might crash somewhere else closer to your problem though
该方法的实施方式存在许多问题。在检查
db
是否可以打开时,您释放了自动释放池,然后继续执行其余代码。我假设您可能希望在此时返回 nil 。在[rs next]
部分中,您使用+array
创建一个NSMutableArray
,这将创建一个自动释放的对象。因此,您不应该调用[usersDictArray release]
,因为这会导致过度释放。 (在每个循环中,“临时”、自动释放的usersDictArray
实例将存储在pool
自动释放池中。当您调用[pool排出]
,自动释放池将向所有这些临时实例发送一条release
消息)。接近尾声时,您会return myDictionary;
,这会导致接下来的 2 行永远无法到达。因此,您创建的自动释放池永远不会被释放(弹出)。这可能就是我实现它的方式:(
请注意,我实现这一点的方式假设创建本地自动释放池的原因是为了性能,并且有一个全局
NSAutoreleasePool
来吸收最终的[myDictionary autorelease]
自动释放对象)。There are numerous issues with how that method is implemented. In the check for whether the
db
could be opened or not, you release the autorelease pool, but then continue on to the rest of the code. I assume you would probably want to returnnil
at that point. In the[rs next]
section, you create anNSMutableArray
with+array
, which creates an autoreleased object. As such, you shouldn't call[usersDictArray release]
, as that would be over-releasing. (In each loop, the "temporary", autoreleasedusersDictArray
instances will be stored in thepool
autorelease pool. When you call[pool drain]
, the autorelease pool will send all of those temporary instances arelease
message). Towards the end, you havereturn myDictionary;
, which causes the next 2 lines to never be reached. As a result, the autorelease pool you created would never be released (popped).This is probably how I would implement it:
(Note that how I implemented this assumes that the reasoning behind creating a local autorelease pool is for performance, and that there is a global
NSAutoreleasePool
in place to absorb the final[myDictionary autorelease]
autoreleased object).调用:(
尝试在使用字典之前 也许在类初始化时)。不要忘记
在 dealloc 处
调用:另请检查标头中的属性声明是否正确。它必须是:
并将
@synthesizeridesDict;
添加到您的类实现中。Try to call:
before using the dictionary(maybe at class init). Don't forget to call:
at dealloc.
Also check if you declare property in the header correctly. It must be:
And add
@synthesize ridesDict;
to your class implementation.