对象没有在iPhone中释放 [英] object not getting released in iphone

查看:82
本文介绍了对象没有在iPhone中释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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,但它将从function返回.我怎么能实现这个数组.请帮帮我.我正在从数据库中获取数据.

I want release arrRecentSearch but it will return from function . How can i realese this array. Please help me.I am fetching data from databse.

推荐答案

只需自动释放它即可:

return [arrRecentSearch autorelease];

请参阅苹果便笺管理指南了解有关其工作原理的更多信息

Have a look at the apple memopry management guidelines for more information on how this works

如果要返回自动释放的对象,则必须记住要保留该对象,以备日后保留.也就是说,如果我们有一个返回自动释放数组的函数

If you are going to return an autoreleased object, you must remember to retain it if you wnat to keep it around later. i.e. if we have a function that returns an autoreleased array

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

并且您想记住搜索结果以备后用,您必须记住:

and you want to remember the search results for later you must remember to do this :

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

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

or, you might use a property to store it :

@property (nonatomic, copy) NSArray *searchResults;

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

无论哪种方式,如果您只是将其保留为自动发布,它将消失,并且您将获得异常!

Either way, if you just leave it as autoreleased, it's going to vanish and you're going to get an exception!

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

Just realised MustISignUp has answered this in a comment!

这篇关于对象没有在iPhone中释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆