NSMutableDictionary calling EXC_BAD_ACCESS

I am trying to fix the EXC_BAD_ACCESS error that occurs when accessing the NSMutableDictionary in tableView: cellForRowAtIndexPath: indexPath. Now it works when I populate ridesDict with the loadHistoryFromDBExtended method as follows:

self.ridesDict = [self loadHistoryFromDBExtended]; 

NSLog(@"rides dict %@", self.ridesDict);

However, I do not want to call [self loadHistoryFromDBExtended] for each load cell, since the dictionary will not change, so I tried to move:

self.ridesDict = [self loadHistoryFromDBExtended]; 

to view DidLoad, and now I get an EXC_BAD_ACCESS error when using:

NSLog(@"rides dict %@", self.ridesDict);

in the table View: cellForRowAtIndexPath: indexPath. From what I read, it seems that I have problems with saving / releasing memory, but I cannot figure it out. I tried [self.ridesDict save] in viewDidLoad after calling the loadHistoryFromDBExtended method, but that didn't help. I am very new to this, so I would appreciate any directions 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];
}
+3
source share
3 answers

, . , db , , . , , , nil . [rs next] NSMutableArray +array, . , [usersDictArray release], . ( "", usersDictArray pool. [pool drain], release). return myDictionary;, , . ().

, :

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

+2

, EXC_BAD_ACCESS

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

  • . ? , , - .

  • NSZombiesEnabled - , , . 0.

  • Guard Malloc, GDB . , , , . ,

+4

:

[self.ridesDict retain];

(, init). :

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

dealloc.

. :

@property (nonatomic, retain) NSMutableArray *ridesDict;

@synthesize ridesDict; .

+1

All Articles