NSMutableDictionary 导致 EXC_BAD_ACCESS

发布于 2024-11-04 07:11:35 字数 1908 浏览 2 评论 0原文

我正在尝试修复当我访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

束缚m 2024-11-11 07:11:35

我写这篇博客是为了帮助理解和调试 EXC_BAD_ACCESS

http://loufranco.com/blog/files /Understanding-EXC_BAD_ACCESS.html

按照容易程度的顺序

  1. 运行构建并分析 - 您是否获得了干净的构建?看看它说的是什么,但您现在可以忽略泄漏问题 - 查找向已发布对象发送消息的问题

  2. 使用 NSZombiesEnabled 运行 - 这使得对象永远不会释放,然后如果将消息发送到对象则抱怨keepCount 为 0。

  3. 启用 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

  1. 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

  2. Run with NSZombiesEnabled -- this makes objects never deallocate, and then complain if a message is sent to an object with retainCount of 0.

  3. 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

楠木可依 2024-11-11 07:11:35

该方法的实施方式存在许多问题。在检查db是否可以打开时,您释放了自动释放池,然后继续执行其余代码。我假设您可能希望在此时返回 nil 。在 [rs next] 部分中,您使用 +array 创建一个 NSMutableArray,这将创建一个自动释放的对象。因此,您不应该调用 [usersDictArray release],因为这会导致过度释放。 (在每个循环中,“临时”、自动释放的 usersDictArray 实例将存储在 pool 自动释放池中。当您调用 [pool排出] ,自动释放池将向所有这些临时实例发送一条 release 消息)。接近尾声时,您会 return myDictionary;,这会导致接下来的 2 行永远无法到达。因此,您创建的自动释放池永远不会被释放(弹出)。

这可能就是我实现它的方式:(

-(NSMutableDictionary *)loadHistoryFromDBExtended {   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    FMDatabase* db = [FMDatabase databaseWithPath:[self getDBPath]];

    if (![db open]) 
    {
        NSLog(@"Could not open db.");
        [pool release];
        return nil; // I'm assuming you should return nil here
    }

    //get users; query provides result set
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM R order by date desc"];

    //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];
    }   
    [pool drain];
    return [myDictionary autorelease];
}

请注意,我实现这一点的方式假设创建本地自动释放池的原因是为了性能,并且有一个全局 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 return nil at that point. In the [rs next] section, you create an NSMutableArray 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", autoreleased usersDictArray instances will be stored in the pool autorelease pool. When you call [pool drain], the autorelease pool will send all of those temporary instances a release message). Towards the end, you have return 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:

-(NSMutableDictionary *)loadHistoryFromDBExtended {   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    FMDatabase* db = [FMDatabase databaseWithPath:[self getDBPath]];

    if (![db open]) 
    {
        NSLog(@"Could not open db.");
        [pool release];
        return nil; // I'm assuming you should return nil here
    }

    //get users; query provides result set
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM R order by date desc"];

    //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];
    }   
    [pool drain];
    return [myDictionary autorelease];
}

(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).

独闯女儿国 2024-11-11 07:11:35

调用:(

[self.ridesDict retain];

尝试在使用字典之前 也许在类初始化时)。不要忘记

[self.ridesDict release];
self.ridesDict = nil;

在 dealloc 处

调用:另请检查标头中的属性声明是否正确。它必须是:

@property (nonatomic, retain) NSMutableArray *ridesDict;

并将 @synthesizeridesDict; 添加到您的类实现中。

Try to call:

[self.ridesDict retain];

before using the dictionary(maybe at class init). Don't forget to call:

[self.ridesDict release];
self.ridesDict = nil;

at dealloc.

Also check if you declare property in the header correctly. It must be:

@property (nonatomic, retain) NSMutableArray *ridesDict;

And add @synthesize ridesDict; to your class implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文