简体   繁体   English

正确使用FMDB /内存

[英]Correct Usage FMDB / Memory

I have not been able to find a solution to my problem in similar questions. 我无法在类似问题中找到解决问题的方法。 I am extracting a whole bunch of data from a sqlite database using the fmdb wrapper. 我正在使用fmdb包装器从sqlite数据库中提取一大堆数据。 I have a method in my fmdb utility class similar to this: 我的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;
}

That stores my info in an array of dictionaries. 那会将我的信息存储在字典中。 In my view controller I make a call to my method like so: 在我的视图控制器中,我这样调用我的方法:

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

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

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

   [ db openDatabase ];

   currentConnections = [ db getConnectionsForKey:key ];

   [ db closeDatabase ];
  [ db release ];


}

Now everytime I call this method to update my array with new contacts, I have a consistent leak that keeps building up as I observe with the allocations tool. 现在,每次我调用此方法以使用新联系人更新数组时,都会出现一致的泄漏,在使用分配工具观察时,泄漏会不断累积。 This happens even when I release currentConnections right after loading from the database and never call this method again so I am suspecting it is something with the database implementation. 即使在我从数据库加载后立即释放currentConnections,再也不再调用此方法时,这种情况才会发生,因此我怀疑这与数据库实现有关。 Has anyone else handled this problem? 还有其他人处理过这个问题吗?

currentConnections is leaking in 'setCurrentConnections'. currentConnections在“ setCurrentConnections”中泄漏。 Here is how I would fix it (notice I also change 'getConnectionsForKey' to return an autoreleased object). 这是解决问题的方法(注意,我还更改了“ 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 ];

}

You can probably skip the call to removeAllObjects and just call release. 您可能可以跳过对removeAllObjects的调用,而只是释放。 The problem in the original code is that you were overwriting the reference to your existing currentConnection without releasing it first, therefore causing the leak. 原始代码中的问题是您正在覆盖对现有currentConnection的引用,而没有先释放它,因此导致泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM