对象没有在iPhone中发布

时间:2010-05-28 09:38:19

标签: iphone memory-leaks

 NSString *strSql = @"select tblrecentsearch_id,xmlrequest,company,postcode,city,kilometer,date from tblrecentsearch";

 returnValue = sqlite3_prepare_v2(database, [strSql UTF8String], -1, &selectStatement, NULL);
 if(returnValue == SQLITE_OK)
 {
  arrRecentSearch=[[NSMutableArray alloc] init];

  while(sqlite3_step(selectStatement)==SQLITE_ROW)
  {
   Search *ObjSearch = [[Search alloc]init];
   ObjSearch.intRecentSearchId = sqlite3_column_int(selectStatement, 0);
   ObjSearch.xmlRequest = [NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 1) encoding:NSUTF8StringEncoding];
   ObjSearch.strCompnay=[NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 2) encoding:NSUTF8StringEncoding];
   ObjSearch.strPostCode=[NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 3) encoding:NSUTF8StringEncoding];
   ObjSearch.strPlace = [NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 4) encoding:NSUTF8StringEncoding];
   ObjSearch.strKilometer = [NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 5) encoding:NSUTF8StringEncoding];
   ObjSearch.strDate = [NSString stringWithCString:(char  *)sqlite3_column_text_check(selectStatement, 6) encoding:NSUTF8StringEncoding];

   [arrRecentSearch addObject:ObjSearch];

   [ObjSearch release];
  }
 }

 sqlite3_finalize(selectStatement);

我想发布arrRecentSearch,但它会从函数返回。我怎样才能重新定位这个数组。请帮帮我。我从数据库中获取数据。

1 个答案:

答案 0 :(得分:1)

只是自动释放它:

return [arrRecentSearch autorelease];

请查看apple memopry management guidelines,了解有关其工作原理的更多信息

如果你要返回一个自动释放的物体,你必须记得保留它,如果你想在以后保留它。即如果我们有一个返回自动释放数组的函数

- (NSArray *) getSearchResults {
    return [[[NSArray alloc] init] autorelease];
}

并且您想要记住搜索结果,以后必须记住这样做:

...
NSArray *results = [[self getSearchResults] retain]; //!< Remember the retain here!
...

或者,您可以使用属性来存储它:

@property (nonatomic, copy) NSArray *searchResults;

...
self.searchResults = [self getSearchResults]; //!< The property handles the retain for you here
...

无论哪种方式,如果你只是将它作为自动释放,它将会消失,你会得到一个例外!

编辑:刚刚意识到MustISignUp在评论中已经回答了这个问题!