释放对象时发出内存警告

时间:2011-12-04 10:51:10

标签: iphone

请参阅以下方法

      -(void)addScrollView{
[self selectData];


scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(5, 00, 320, 480)];
int counter=5;
float y=40.0f;
int fullLength=[photoArray count];
int horizontal=320;
int vertical=(fullLength/4)*80;
int c1=1;
for(int c=0;c<[photoArray count];c++){
    PhotoData *d=[photoArray objectAtIndex:c];
    //NSLog(d.photoPath);
    if(c1==5){
        counter=5;
        y=y+80.0f;
        c1=1;

    }
    UIImage *img1=[[UIImage alloc]initWithContentsOfFile:d.photoPath];
    UIButton* button = [[UIButton alloc] init];
    button.tag=c;

    [button setBackgroundImage:img1 forState:UIControlStateNormal];
        [button setFrame:CGRectMake(counter, y, 70.0, 70.0)];

    [button addTarget:self action:@selector(showDetail:)
     forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];
    counter=counter+78.0f;
    c1++;
    [img1 release];
    [button release];

}
[scrollView setContentSize:CGSizeMake(horizontal, vertical+200)];


  [self.view addSubview:scrollView];
     [scrollView release];

    }

             -(void)selectData{
//This method is defined to retrieve data from Database
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
//Obtained the path of Documennt directory  which is editable

NSString *filePath = [documentsPath stringByAppendingPathComponent:@"memory.sql"];
//memory.sql is sqlite file which is used as local database
photoArray=[[NSMutableArray alloc]init];


NSString *dbPath=filePath;

sqlite3 *database;

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from photo ";
    sqlite3_stmt *compiledStatement;


    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        //sqlite3_bind_int(compiledStatement, 1,memoryId);

        //(compiledStatement, 1, [header UTF8String], -1, SQLITE_TRANSIENT);

        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {              
            PhotoData *data=[[PhotoData alloc]init];
            //create the MemoryData object to store the data of one record

            //NSLog(@"Data is retrieved using mid=%i",memoryId);
            // Read the data from the result row

            int pId=sqlite3_column_int(compiledStatement, 1);

            NSString *filePath=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            ///filePath=[self retrievePath:filePath];

            [data setPhotoId:pId];
            [data setPhotoPath:filePath];
            //Store every object of MemoryData in t
            [photoArray addObject:data];
            [filePath release];


        } // end of the while


    }
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
NSLog(@"size of array is %i",[photoArray count]);
tableArray=[[NSArray alloc]initWithArray:photoArray];
//convert the MutableArray to NSArray


    }

如您所见,我正在释放所有对象,为什么会出现以下错误

  2011-12-04 14:48:21.367 Memorable[110:707] Received memory warning. Level=2
  2011-12-04 14:48:22.084 Memorable[110:707] Received memory warning. Level=2
  2011-12-04 14:48:22.247 Memorable[110:707] Received memory warning. Level=2
  2011-12-04 14:48:22.255 Memorable[110:707] Received memory warning. Level=2
  2011-12-04 14:48:23.507 Memorable[110:707] Received memory warning. Level=1
 2011-12-04 14:48:27.188 Memorable[110:707] Received memory warning. Level=1
  Program received signal:  “EXC_BAD_ACCESS”.
 warning: Unable to read symbols for /xcode3/Platforms/iPhoneOS.platform/DeviceSupport/4.3.1 (8G4)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

2 个答案:

答案 0 :(得分:2)

  1. 首先你不应该发布filePath, 方法[NSString stringWithUTF8String...返回已自动释放的对象。
  2. 您使用PhotoData创建alloc对象,因此您需要 发布/自动发布

        NSString *filePath=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
    
        [data setPhotoId:pId];
        [data setPhotoPath:filePath];
        [photoArray addObject:data];
        //[filePath release]; <-crash here
        [data release];  // <-leaks were here
    

答案 1 :(得分:0)

我没有看到你在任何地方发布数据对象。

编辑:

因为铍声明不释放filepath,因为你不拥有它。它是一个自动释放对象。