我无法在类似问题中找到解决问题的方法。我正在使用fmdb包装器从sqlite数据库中提取一大堆数据。我的fmdb实用程序类中有一个与此类似的方法:

  - (NSMutableArray *) getConnectionsForKey: (NSString *) key
 {

     NSString *query = [NSString stringWithFormat:@"select * from user" ];



     FMResultSet *results = [database executeQuery:query];

     NSMutableArray *retArray = [[ NSMutableArray alloc ] init ];

     while ([ results next ]) {

         NSString *fname = [ results stringForColumn:@"firstname" ];
         NSString *lname = [ results stringForColumn:@"lastname" ];


         NSMutableDictionary *aDictionary = [[ NSMutableDictionary alloc ]
                                               init
                                             ];

        [ aDictionary
          setObject:fname
          forKey:@"firstname"
        ];

       [ aDictionary
        setObject:lname
        forKey:@"lastname"
       ];



       [ retArray addObject:aDictionary ];

       [ aDictionary release ];


}

[ results close ];

return retArray;
}


那会将我的信息存储在一系列字典中。在我的视图控制器中,我这样调用我的方法:

 -(void) setCurrentConnections: (NSString *) key {

   if ( currentConnections != nil ) {
         [ currentConnections removeAllObjects ];
   }

   LocalDatabase *db = [[ LocalDatabase alloc ] init ];

   [ db openDatabase ];

   currentConnections = [ db getConnectionsForKey:key ];

   [ db closeDatabase ];
  [ db release ];


}


现在,每次我调用此方法以使用新联系人更新数组时,都会出现一致的泄漏,并且随着分配工具的观察,泄漏不断增加。即使在我从数据库加载后立即释放currentConnections却再也没有调用此方法时,这种情况才会发生,因此我怀疑这与数据库实现有关。还有其他人处理过这个问题吗?

最佳答案

currentConnections在“ setCurrentConnections”中泄漏。这是解决问题的方法(注意,我还更改了“ getConnectionsForKey”以返回自动释放的对象)。

 - (NSMutableArray *) getConnectionsForKey: (NSString *) key
 {

     NSString *query = [NSString stringWithFormat:@"select * from user" ];
     FMResultSet *results = [database executeQuery:query];

     NSMutableArray *retArray = [[ NSMutableArray alloc ] init ];
     while ([ results next ]) {

         NSString *fname = [ results stringForColumn:@"firstname" ];
         NSString *lname = [ results stringForColumn:@"lastname" ];
         NSMutableDictionary *aDictionary = [[ NSMutableDictionary alloc ] init];

        [ aDictionary
          setObject:fname
          forKey:@"firstname"
        ];

       [ aDictionary
        setObject:lname
        forKey:@"lastname"
       ];

       [ retArray addObject:aDictionary ];
       [ aDictionary release ];


       }

[ results close ];

return [retArray autorelease];
}


 -(void) setCurrentConnections: (NSString *) key {

   [ currentConnections removeAllObjects ];
   [ currentConnections release];

   LocalDatabase *db = [[ LocalDatabase alloc ] init ];

   [ db openDatabase ];

   currentConnections = [ db getConnectionsForKey:key ];
   [currentConnections retain];

   [ db closeDatabase ];
  [ db release ];

}


您可能可以跳过对removeAllObjects的呼叫,而只是释放呼叫。原始代码中的问题是您正在覆盖对现有currentConnection的引用,而没有先释放它,因此会导致泄漏。

关于objective-c - 正确使用FMDB/内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9103208/

10-10 20:53